|
| 1 | +/* eslint-disable no-use-before-define */ |
| 2 | + |
| 3 | +const mkdirp = require('mkdirp'); |
| 4 | +const request = require('request'); |
| 5 | +const tar = require('tar'); |
| 6 | + |
| 7 | +const createApp = require('frint').createApp; |
| 8 | + |
| 9 | +const DEFAULT_ORG = 'frintjs'; |
| 10 | +const DEFAULT_REPO = 'frint'; |
| 11 | +const DEFAULT_BRANCH = 'master'; |
| 12 | +const DEFAULT_EXAMPLES_DIR = 'examples'; |
| 13 | +const DEFAULT_EXAMPLE = 'counter'; |
| 14 | +const DESCRIPTION_TEXT = ` |
| 15 | +Usage: |
| 16 | +
|
| 17 | + $ frint new |
| 18 | + $ frint new <directory> |
| 19 | + $ frint new <directory> --example <example> |
| 20 | +
|
| 21 | +Example: |
| 22 | +
|
| 23 | + $ frint new myapp --example kitchensink |
| 24 | + $ frint new myapp --example frint-vue/tree/master/examples/basic |
| 25 | +
|
| 26 | +You can find a list of all available official examples here: |
| 27 | +https://github.com/frintjs/frint/tree/master/examples |
| 28 | +`.trim(); |
| 29 | +const INVALID_EXAMPLE_ARG_TEXT = ` |
| 30 | +Invalid <example> value. Must be in one of the following formats: |
| 31 | +
|
| 32 | + * <name> |
| 33 | + * <organization>/<repository>/tree/<branch>/** |
| 34 | +`.trim(); |
| 35 | +const COMPLETION_TEXT = ` |
| 36 | +Done! |
| 37 | +
|
| 38 | +Please run these two commands to start your application: |
| 39 | +{} |
| 40 | + $ npm install |
| 41 | + $ npm start |
| 42 | +`.trim(); |
| 43 | + |
| 44 | +module.exports = createApp({ |
| 45 | + name: 'new', |
| 46 | + providers: [ |
| 47 | + { |
| 48 | + name: 'summary', |
| 49 | + useValue: 'Scaffolds a new Frint app in specified directory', |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'description', |
| 53 | + useValue: DESCRIPTION_TEXT, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'execute', |
| 57 | + useFactory: function useFactory(deps) { |
| 58 | + return function execute() { |
| 59 | + deps.console.log('Initializing...'); |
| 60 | + Promise.resolve(deps) |
| 61 | + .then(mapDepsToCtx) |
| 62 | + .then(createOutputDir) |
| 63 | + .then(streamExampleToOutputDir) |
| 64 | + .then(ctx => deps.console.log(getCompletionText(ctx))) |
| 65 | + .catch(deps.console.error); |
| 66 | + }; |
| 67 | + }, |
| 68 | + deps: [ |
| 69 | + 'console', |
| 70 | + 'params', |
| 71 | + 'pwd', |
| 72 | + ], |
| 73 | + } |
| 74 | + ], |
| 75 | +}); |
| 76 | + |
| 77 | +function mapDepsToCtx(deps) { |
| 78 | + return new Promise((resolve, reject) => { |
| 79 | + // The <example> param has two shapes: |
| 80 | + // * <name> - example name from the official Frint GitHub repository |
| 81 | + // * <organization>/<repo>/tree/<branch>/** - full GitHub path to an arbitrary example |
| 82 | + const example = deps.params.example || DEFAULT_EXAMPLE; |
| 83 | + if (!example.match(/(^(\w|-)+$)|^\/?(\w|-)+(\/(\w|-)+){3,}\/?$/)) { |
| 84 | + reject(INVALID_EXAMPLE_ARG_TEXT); |
| 85 | + } |
| 86 | + |
| 87 | + // If <directory> is specified, it is taken as the 1st value from params _ array. |
| 88 | + // Note that this array does not include the <example> flag. |
| 89 | + const isOutputCurrentDir = deps.params._.length === 0; |
| 90 | + |
| 91 | + const ctx = { |
| 92 | + isOutputCurrentDir, |
| 93 | + outputDir: isOutputCurrentDir ? deps.pwd : deps.params._[0], |
| 94 | + }; |
| 95 | + |
| 96 | + const isCustomExample = example.indexOf('/') >= 0; |
| 97 | + if (isCustomExample) { |
| 98 | + populateCtxForCustomRepo(ctx, example); |
| 99 | + } else { |
| 100 | + populateCtxForDefaultRepo(ctx, example); |
| 101 | + } |
| 102 | + |
| 103 | + resolve(ctx); |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +function populateCtxForCustomRepo(ctx, example) { |
| 108 | + // Split by '/' and filter out empty results. |
| 109 | + // <example> arg might start or end with a separator. |
| 110 | + const exampleParts = example.split('/').filter(str => str !== ''); |
| 111 | + ctx.org = exampleParts[0]; |
| 112 | + ctx.repo = exampleParts[1]; |
| 113 | + ctx.branch = exampleParts[3]; |
| 114 | + ctx.examplePath = exampleParts.slice(4).join('/'); |
| 115 | +} |
| 116 | + |
| 117 | +function populateCtxForDefaultRepo(ctx, example) { |
| 118 | + ctx.org = DEFAULT_ORG; |
| 119 | + ctx.repo = DEFAULT_REPO; |
| 120 | + ctx.branch = DEFAULT_BRANCH; |
| 121 | + ctx.examplePath = `${DEFAULT_EXAMPLES_DIR}/${example}`; |
| 122 | +} |
| 123 | + |
| 124 | +function createOutputDir(ctx) { |
| 125 | + return new Promise((resolve, reject) => { |
| 126 | + mkdirp(ctx.outputDir, function mkdirpCallback(error) { |
| 127 | + if (error) { |
| 128 | + reject(error); |
| 129 | + return; |
| 130 | + } |
| 131 | + resolve(ctx); |
| 132 | + }); |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +function streamExampleToOutputDir(ctx) { |
| 137 | + return new Promise((resolve, reject) => { |
| 138 | + request(`https://codeload.github.com/${ctx.org}/${ctx.repo}/tar.gz/${ctx.branch}`) |
| 139 | + .on('error', reject) |
| 140 | + .pipe(tar.x({ |
| 141 | + filter: p => p.indexOf(`${ctx.repo}-${ctx.branch}/${ctx.examplePath}/`) === 0, |
| 142 | + strip: 3, |
| 143 | + C: ctx.outputDir, |
| 144 | + })) |
| 145 | + .on('error', reject) |
| 146 | + .on('finish', resolve(ctx)); |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +function getCompletionText(ctx) { |
| 151 | + return COMPLETION_TEXT.replace( |
| 152 | + "{}", |
| 153 | + ctx.isOutputCurrentDir ? "" : `\n $ cd ${ctx.outputDir}`); |
| 154 | +} |
0 commit comments