Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 01a50a1

Browse files
fix(install): Ensure install command doesn't run default command (#6)
Fixes #5
1 parent d358814 commit 01a50a1

File tree

1 file changed

+59
-61
lines changed

1 file changed

+59
-61
lines changed

bin/cli.js

+59-61
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ const writeFileAsync = promisify(fs.writeFile);
1717
const configPath = findUp.sync(['html-sketchapp.config.js']);
1818
const config = configPath ? require(configPath) : {};
1919

20-
const argv = require('yargs')
20+
const makeServer = (relativePath, port) => {
21+
const servePath = path.resolve(process.cwd(), relativePath);
22+
return serve(servePath, { port, silent: true });
23+
};
24+
25+
require('yargs')
2126
.config(config)
2227
.config('config', 'Path to JavaScript config file', customConfigPath => require(customConfigPath))
2328
.usage('Usage: $0 [options]')
@@ -43,79 +48,72 @@ const argv = require('yargs')
4348
alias: 'v',
4449
describe: 'Set of named viewport sizes for symbols, e.g. --viewports.Desktop=1024x768 --viewports.Mobile=320x568'
4550
}
46-
})
47-
.command('install', 'Install the html-sketchapp Sketch plugin', {}, async () => {
48-
const { getInstalledPath } = require('get-installed-path');
49-
const htmlSketchappPath = await getInstalledPath('html-sketchapp');
50-
const pluginPath = path.resolve(htmlSketchappPath, 'asketch2sketch.sketchplugin');
51-
52-
const opn = require('opn');
53-
opn(pluginPath);
54-
})
55-
.argv;
51+
}, async argv => {
52+
const port = argv.serve ? await getPort() : null;
53+
const server = argv.serve ? makeServer(argv.serve, port) : null;
5654

57-
const makeServer = (relativePath, port) => {
58-
const servePath = path.resolve(process.cwd(), relativePath);
59-
return serve(servePath, { port, silent: true });
60-
};
61-
62-
(async function() {
63-
const port = argv.serve ? await getPort() : null;
64-
const server = argv.serve ? makeServer(argv.serve, port) : null;
65-
66-
try {
67-
const url = argv.file ? `file://${path.join(process.cwd(), argv.file)}` : argv.url;
68-
const symbolsUrl = argv.serve ? urlJoin(`http://localhost:${String(port)}`, argv.url || '/') : url;
69-
await waitOn({ resources: [symbolsUrl] });
55+
try {
56+
const url = argv.file ? `file://${path.join(process.cwd(), argv.file)}` : argv.url;
57+
const symbolsUrl = argv.serve ? urlJoin(`http://localhost:${String(port)}`, argv.url || '/') : url;
58+
await waitOn({ resources: [symbolsUrl] });
7059

71-
const browser = await puppeteer.launch();
60+
const browser = await puppeteer.launch();
7261

73-
try {
74-
const page = await browser.newPage();
62+
try {
63+
const page = await browser.newPage();
7564

76-
await page.goto(symbolsUrl, { waitUntil: 'networkidle0' });
65+
await page.goto(symbolsUrl, { waitUntil: 'networkidle0' });
7766

78-
const bundlePath = path.resolve(__dirname, '../script/dist/generateAlmostSketch.bundle.js');
79-
const bundle = await readFileAsync(bundlePath, 'utf8');
80-
await page.addScriptTag({ content: bundle });
67+
const bundlePath = path.resolve(__dirname, '../script/dist/generateAlmostSketch.bundle.js');
68+
const bundle = await readFileAsync(bundlePath, 'utf8');
69+
await page.addScriptTag({ content: bundle });
8170

82-
await page.evaluate('generateAlmostSketch.setupSymbols({ name: "html-sketchapp symbols" })');
71+
await page.evaluate('generateAlmostSketch.setupSymbols({ name: "html-sketchapp symbols" })');
8372

84-
await page.evaluate('generateAlmostSketch.snapshotColorStyles()');
73+
await page.evaluate('generateAlmostSketch.snapshotColorStyles()');
8574

86-
const viewports = argv.viewports || { Desktop: '1024x768' };
87-
const hasViewports = Object.keys(viewports).length > 1;
88-
for (const viewportName in viewports) {
89-
if (viewports.hasOwnProperty(viewportName)) {
90-
const viewport = viewports[viewportName];
91-
const [ width, height ] = viewport.split('x').map(x => parseInt(x, 10));
92-
await page.setViewport({ width, height });
93-
await page.evaluate(`generateAlmostSketch.snapshotTextStyles({ suffix: "${hasViewports ? `/${viewportName}` : ''}" })`);
94-
await page.evaluate(`generateAlmostSketch.snapshotSymbols({ suffix: "${hasViewports ? `/${viewportName}` : ''}" })`);
75+
const viewports = argv.viewports || { Desktop: '1024x768' };
76+
const hasViewports = Object.keys(viewports).length > 1;
77+
for (const viewportName in viewports) {
78+
if (viewports.hasOwnProperty(viewportName)) {
79+
const viewport = viewports[viewportName];
80+
const [ width, height ] = viewport.split('x').map(x => parseInt(x, 10));
81+
await page.setViewport({ width, height });
82+
await page.evaluate(`generateAlmostSketch.snapshotTextStyles({ suffix: "${hasViewports ? `/${viewportName}` : ''}" })`);
83+
await page.evaluate(`generateAlmostSketch.snapshotSymbols({ suffix: "${hasViewports ? `/${viewportName}` : ''}" })`);
84+
}
9585
}
96-
}
9786

98-
const asketchDocumentJSON = await page.evaluate('generateAlmostSketch.getDocumentJSON()');
99-
const asketchPageJSON = await page.evaluate('generateAlmostSketch.getPageJSON()');
87+
const asketchDocumentJSON = await page.evaluate('generateAlmostSketch.getDocumentJSON()');
88+
const asketchPageJSON = await page.evaluate('generateAlmostSketch.getPageJSON()');
10089

101-
const outputPath = path.resolve(process.cwd(), argv.outDir);
102-
await mkdirp(outputPath);
90+
const outputPath = path.resolve(process.cwd(), argv.outDir);
91+
await mkdirp(outputPath);
10392

104-
const outputPagePath = path.join(outputPath, 'page.asketch.json');
105-
const outputDocumentPath = path.join(outputPath, 'document.asketch.json');
93+
const outputPagePath = path.join(outputPath, 'page.asketch.json');
94+
const outputDocumentPath = path.join(outputPath, 'document.asketch.json');
10695

107-
await Promise.all([
108-
writeFileAsync(outputPagePath, asketchPageJSON),
109-
writeFileAsync(outputDocumentPath, asketchDocumentJSON)
110-
]);
96+
await Promise.all([
97+
writeFileAsync(outputPagePath, asketchPageJSON),
98+
writeFileAsync(outputDocumentPath, asketchDocumentJSON)
99+
]);
100+
} finally {
101+
if (browser && typeof browser.close === 'function') {
102+
browser.close();
103+
}
104+
}
111105
} finally {
112-
if (browser && typeof browser.close === 'function') {
113-
browser.close();
106+
if (server && typeof server.stop === 'function') {
107+
server.stop();
114108
}
115109
}
116-
} finally {
117-
if (server && typeof server.stop === 'function') {
118-
server.stop();
119-
}
120-
}
121-
}());
110+
})
111+
.command('install', 'Install the html-sketchapp Sketch plugin', {}, async () => {
112+
const { getInstalledPath } = require('get-installed-path');
113+
const htmlSketchappPath = await getInstalledPath('html-sketchapp');
114+
const pluginPath = path.resolve(htmlSketchappPath, 'asketch2sketch.sketchplugin');
115+
116+
const opn = require('opn');
117+
opn(pluginPath, { wait: false });
118+
})
119+
.parse();

0 commit comments

Comments
 (0)