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

Commit

Permalink
fix(install): Ensure install command doesn't run default command (#6)
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
markdalgleish authored Dec 19, 2017
1 parent d358814 commit 01a50a1
Showing 1 changed file with 59 additions and 61 deletions.
120 changes: 59 additions & 61 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ const writeFileAsync = promisify(fs.writeFile);
const configPath = findUp.sync(['html-sketchapp.config.js']);
const config = configPath ? require(configPath) : {};

const argv = require('yargs')
const makeServer = (relativePath, port) => {
const servePath = path.resolve(process.cwd(), relativePath);
return serve(servePath, { port, silent: true });
};

require('yargs')
.config(config)
.config('config', 'Path to JavaScript config file', customConfigPath => require(customConfigPath))
.usage('Usage: $0 [options]')
Expand All @@ -43,79 +48,72 @@ const argv = require('yargs')
alias: 'v',
describe: 'Set of named viewport sizes for symbols, e.g. --viewports.Desktop=1024x768 --viewports.Mobile=320x568'
}
})
.command('install', 'Install the html-sketchapp Sketch plugin', {}, async () => {
const { getInstalledPath } = require('get-installed-path');
const htmlSketchappPath = await getInstalledPath('html-sketchapp');
const pluginPath = path.resolve(htmlSketchappPath, 'asketch2sketch.sketchplugin');

const opn = require('opn');
opn(pluginPath);
})
.argv;
}, async argv => {
const port = argv.serve ? await getPort() : null;
const server = argv.serve ? makeServer(argv.serve, port) : null;

const makeServer = (relativePath, port) => {
const servePath = path.resolve(process.cwd(), relativePath);
return serve(servePath, { port, silent: true });
};

(async function() {
const port = argv.serve ? await getPort() : null;
const server = argv.serve ? makeServer(argv.serve, port) : null;

try {
const url = argv.file ? `file://${path.join(process.cwd(), argv.file)}` : argv.url;
const symbolsUrl = argv.serve ? urlJoin(`http://localhost:${String(port)}`, argv.url || '/') : url;
await waitOn({ resources: [symbolsUrl] });
try {
const url = argv.file ? `file://${path.join(process.cwd(), argv.file)}` : argv.url;
const symbolsUrl = argv.serve ? urlJoin(`http://localhost:${String(port)}`, argv.url || '/') : url;
await waitOn({ resources: [symbolsUrl] });

const browser = await puppeteer.launch();
const browser = await puppeteer.launch();

try {
const page = await browser.newPage();
try {
const page = await browser.newPage();

await page.goto(symbolsUrl, { waitUntil: 'networkidle0' });
await page.goto(symbolsUrl, { waitUntil: 'networkidle0' });

const bundlePath = path.resolve(__dirname, '../script/dist/generateAlmostSketch.bundle.js');
const bundle = await readFileAsync(bundlePath, 'utf8');
await page.addScriptTag({ content: bundle });
const bundlePath = path.resolve(__dirname, '../script/dist/generateAlmostSketch.bundle.js');
const bundle = await readFileAsync(bundlePath, 'utf8');
await page.addScriptTag({ content: bundle });

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

await page.evaluate('generateAlmostSketch.snapshotColorStyles()');
await page.evaluate('generateAlmostSketch.snapshotColorStyles()');

const viewports = argv.viewports || { Desktop: '1024x768' };
const hasViewports = Object.keys(viewports).length > 1;
for (const viewportName in viewports) {
if (viewports.hasOwnProperty(viewportName)) {
const viewport = viewports[viewportName];
const [ width, height ] = viewport.split('x').map(x => parseInt(x, 10));
await page.setViewport({ width, height });
await page.evaluate(`generateAlmostSketch.snapshotTextStyles({ suffix: "${hasViewports ? `/${viewportName}` : ''}" })`);
await page.evaluate(`generateAlmostSketch.snapshotSymbols({ suffix: "${hasViewports ? `/${viewportName}` : ''}" })`);
const viewports = argv.viewports || { Desktop: '1024x768' };
const hasViewports = Object.keys(viewports).length > 1;
for (const viewportName in viewports) {
if (viewports.hasOwnProperty(viewportName)) {
const viewport = viewports[viewportName];
const [ width, height ] = viewport.split('x').map(x => parseInt(x, 10));
await page.setViewport({ width, height });
await page.evaluate(`generateAlmostSketch.snapshotTextStyles({ suffix: "${hasViewports ? `/${viewportName}` : ''}" })`);
await page.evaluate(`generateAlmostSketch.snapshotSymbols({ suffix: "${hasViewports ? `/${viewportName}` : ''}" })`);
}
}
}

const asketchDocumentJSON = await page.evaluate('generateAlmostSketch.getDocumentJSON()');
const asketchPageJSON = await page.evaluate('generateAlmostSketch.getPageJSON()');
const asketchDocumentJSON = await page.evaluate('generateAlmostSketch.getDocumentJSON()');
const asketchPageJSON = await page.evaluate('generateAlmostSketch.getPageJSON()');

const outputPath = path.resolve(process.cwd(), argv.outDir);
await mkdirp(outputPath);
const outputPath = path.resolve(process.cwd(), argv.outDir);
await mkdirp(outputPath);

const outputPagePath = path.join(outputPath, 'page.asketch.json');
const outputDocumentPath = path.join(outputPath, 'document.asketch.json');
const outputPagePath = path.join(outputPath, 'page.asketch.json');
const outputDocumentPath = path.join(outputPath, 'document.asketch.json');

await Promise.all([
writeFileAsync(outputPagePath, asketchPageJSON),
writeFileAsync(outputDocumentPath, asketchDocumentJSON)
]);
await Promise.all([
writeFileAsync(outputPagePath, asketchPageJSON),
writeFileAsync(outputDocumentPath, asketchDocumentJSON)
]);
} finally {
if (browser && typeof browser.close === 'function') {
browser.close();
}
}
} finally {
if (browser && typeof browser.close === 'function') {
browser.close();
if (server && typeof server.stop === 'function') {
server.stop();
}
}
} finally {
if (server && typeof server.stop === 'function') {
server.stop();
}
}
}());
})
.command('install', 'Install the html-sketchapp Sketch plugin', {}, async () => {
const { getInstalledPath } = require('get-installed-path');
const htmlSketchappPath = await getInstalledPath('html-sketchapp');
const pluginPath = path.resolve(htmlSketchappPath, 'asketch2sketch.sketchplugin');

const opn = require('opn');
opn(pluginPath, { wait: false });
})
.parse();

0 comments on commit 01a50a1

Please sign in to comment.