dashboard/shell/creators/app/init

102 lines
2.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
const path = require('path');
const fs = require('fs-extra');
const targets = {
dev: 'NODE_ENV=dev ./node_modules/.bin/vue-cli-service serve',
build: './node_modules/.bin/vue-cli-service build',
clean: './node_modules/@rancher/shell/scripts/clean'
};
const files = [
'tsconfig.json',
'vue.config.js',
'.gitignore',
'.eslintignore',
'.eslintrc.js',
'babel.config.js',
'.vscode/settings.json'
];
console.log('');
console.log('Creating Skeleton Application');
const args = process.argv;
let appFolder = path.resolve('.');
if (args.length === 3) {
const name = args[2];
const folder = path.resolve('.');
appFolder = path.join(folder, name);
console.log(` Creating application folder: ${ appFolder }`);
fs.ensureDirSync(appFolder);
}
// Check that there is a package file
let setName = false;
if (!fs.existsSync(path.join(appFolder, './package.json'))) {
console.log(' Adding package.json');
fs.copySync(path.join(__dirname, 'app.package.json'), path.join(appFolder, 'package.json'));
setName = true;
}
const rawdata = fs.readFileSync(path.join(appFolder, 'package.json'));
const pkg = JSON.parse(rawdata);
if (!pkg.scripts) {
pkg.scripts = {};
}
if (setName) {
const dirName = path.basename(appFolder);
pkg.name = dirName;
}
Object.keys(targets).forEach((target) => {
if (!pkg.scripts[target]) {
pkg.scripts[target] = targets[target];
console.log(` Adding script '${ target }' to package.json`);
}
});
// Add dependencies
// Use the same shell version as this app creator
const creatorPkgData = fs.readFileSync(path.join(__dirname, 'package.json'));
const creatorPkg = JSON.parse(creatorPkgData);
pkg.dependencies['@rancher/shell'] = `^${ creatorPkg.version }`;
// Rest of dependencies are in the _pkgs property of package.json - copy then across
if (creatorPkg._pkgs) {
Object.keys(creatorPkg._pkgs).forEach((pkgName) => {
pkg.dependencies[pkgName] = creatorPkg._pkgs[pkgName];
});
}
fs.writeFileSync(path.join(appFolder, 'package.json'), JSON.stringify(pkg, null, 2));
// Copy base files
files.forEach((file) => {
const src = path.join(__dirname, 'files', file);
const dest = path.join(appFolder, file);
if (!fs.existsSync(dest)) {
console.log(` Adding file: ${ file }`);
const folder = path.dirname(file);
fs.ensureDirSync(folder);
// Create the folder if needed
fs.copySync(src, dest);
}
});
console.log('');