sdkscript
Version:
Set up a modern Cosmos app by running one command ⚛️
158 lines (154 loc) • 6.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createGitApp = void 0;
const shell = __importStar(require("shelljs"));
const c = __importStar(require("ansi-colors"));
const prompt_1 = require("./prompt");
const path_1 = require("path");
const mkdirp_1 = require("mkdirp");
const glob_1 = require("glob");
const fs = __importStar(require("fs"));
const utils_1 = require("./utils");
const createGitApp = (repo) => {
return async (argv) => {
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');
return shell.exit(1);
}
if (!shell.which('yarn')) {
shell.echo('Sorry, this script requires yarn');
return shell.exit(1);
}
let { name } = await (0, prompt_1.prompt)([
{
name: 'name',
message: 'Enter your new app name',
required: true,
}
], argv);
name = name.replace(/\s/g, '-');
const folderName = await (0, utils_1.getTemplateFolder)(argv);
const currentDirectory = process.cwd();
const dir = (0, utils_1.cloneRepo)(argv, repo, name);
// cd into the cloned repo from $dir
shell.cd(name);
// get template
const list = shell.ls(`./${folderName}`);
const { template } = await (0, prompt_1.prompt)([
{
type: 'list',
name: 'template',
message: 'which template',
required: true,
choices: list
}
], argv);
argv.template = template;
const results = await (0, utils_1.getQuestionsAndAnswers)(argv, name, folderName);
const hasResults = Object.keys(results).length > 0;
let license = {};
let scopedResults = {};
if (hasResults) {
({
license,
scopedResults
} = await (0, utils_1.getPackageLicAndAccessInfo)(results));
const path = (0, path_1.join)(folderName, argv.template, '.questions.json');
shell.rm('-rf', path);
}
const files = []
.concat((0, glob_1.sync)((0, path_1.join)(process.cwd(), folderName, template, '/**/.*')))
.concat((0, glob_1.sync)((0, path_1.join)(process.cwd(), folderName, template, '/**/*')));
for (let i = 0; i < files.length; i++) {
const templateFile = files[i];
if (fs.lstatSync(templateFile).isDirectory())
continue;
let content = fs.readFileSync(templateFile).toString();
// LICENSE
if ((0, path_1.basename)(templateFile) === 'LICENSE' &&
license.__LICENSE__ === 'closed') {
content = `Copyright (c) ${new Date().getFullYear()} __USERFULLNAME__ <__USEREMAIL__> - All Rights Reserved
Unauthorized copying via any medium is strictly prohibited
Proprietary and confidential`;
}
// swap out content from results!
Object.keys(results).forEach(key => {
if (/^__/.test(key)) {
content = content.replace(new RegExp(key, 'g'), results[key]);
}
});
// access
if (hasResults) {
if (results.__ACCESS__ === 'public') {
if (scopedResults.scoped) {
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `@${results.__USERNAME__}/${results.__MODULENAME__}`);
}
else {
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `${results.__MODULENAME__}`);
}
}
else {
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `@${results.__USERNAME__}/${results.__MODULENAME__}`);
}
}
// write stuff
const localfile = templateFile.split(`${folderName}/` + template)[1];
const localdir = (0, path_1.dirname)(localfile);
const dirpath = (0, path_1.join)(currentDirectory, name, localdir);
const filepath = (0, path_1.join)(currentDirectory, name, localfile);
(0, mkdirp_1.sync)(dirpath);
fs.writeFileSync(filepath, content);
}
shell.cd(currentDirectory);
shell.rm('-rf', dir);
shell.cd(`./${name}`);
// clean up lock-file business...
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
delete pkg.scripts['locks:remove'];
delete pkg.scripts['locks:create'];
delete pkg.scripts['locks'];
delete pkg.devDependencies['generate-lockfile'];
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2));
// now yarn...
shell.exec(`yarn`);
shell.cd(currentDirectory);
let cmd = `cd ./${name}`;
if (!hasResults) {
cmd += ' && yarn dev';
}
console.log(`
| _ _
=== |.===. '\\-//\`
(o o) {}o o{} (o o)
ooO--(_)--Ooo-ooO--(_)--Ooo-ooO--(_)--Ooo-
✨ Have fun! Now you can start on your project ⚛️
Now, run this command:
${c.bold.whiteBright(cmd)}
`);
};
};
exports.createGitApp = createGitApp;
//# sourceMappingURL=git-cca-template.js.map