2020-01-16 21:11:59 +01:00

83 lines
1.9 KiB
JavaScript

/* eslint-disable no-sync */
/* eslint-disable no-console */
const fs = require ('fs-extra');
const path = require ('path');
const child_process = require ('child_process');
/**
* copies the full template to a new folder named after arg[0]
*
* @param {string} folder folder to run in
* @param {Array} args function arguments
*/
async function run (folder, args) {
const snip_folder = path.join (folder, ...args);
const template = path.join (__dirname, 'template');
for (const f of fs.readdirSync (template))
fs.copy (
path.join (template, f),
path.join (snip_folder, f),
{ filter: (src, dest) => !fs.existsSync (dest) }
);
const devdeps = [
'babel-eslint',
'vue-template-compiler',
'@vue/cli-plugin-babel',
'@vue/cli-plugin-eslint',
'@vue/cli-service'
];
const deps = [
'core-js',
'vue'
];
child_process.execSync (
`npm i --save ${deps.join (' ')} && npm i --save-dev ${devdeps.join (' ')}
`,
{ cwd: snip_folder, stdio: 'inherit' }
);
const package_json = JSON.parse (
await fs.readFile (path.join (snip_folder, 'package.json'), 'utf-8')
);
package_json.scripts.serve = 'vue-cli-service serve';
package_json.scripts.build = 'vue-cli-service build';
await fs.writeFile (
path.join (snip_folder, 'package.json'),
JSON.stringify (package_json, null, 2)
);
}
/**
* checks if the arguments meet the requirements
*
* @param {string} folder folder to run in
* @returns {boolean} true if arguments match requirements
*/
function assert (folder) {
const tests = [
{
f: () => (typeof folder === 'string'),
reason: 'cwd is not a folder (internal error)'
},
{
f: () => (fs.existsSync (folder)),
reason: 'cwd does not exist (internal error)'
}
];
for (const test of tests)
if (!test.f ()) {
console.log (test.reason);
return false;
}
return true;
}
module.exports = { run, assert };