xrmcli
Version:
A suite of cli tools to support Xrm/D365/Dataverse development
72 lines • 3.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const node_fetch_1 = __importDefault(require("node-fetch"));
const fs_extra_1 = require("fs-extra");
const unzipper_1 = require("unzipper");
const child_process_1 = __importDefault(require("child_process"));
commander_1.program.name('xrmcli typescript create');
commander_1.program
.option('--name <name>', 'Project name', 'Webresources')
.requiredOption('--template <template>', 'Template type (tsc, webpack, esbuild)')
.option('--xrmtypesgen', 'Include XrmTypesGen', false)
.option('--path <path>', 'Path to create project', './')
.action(async (options) => {
try {
const { name, template, xrmtypesgen, path } = options;
console.log(path);
const response = await (0, node_fetch_1.default)('https://api.github.com/search/repositories?q=xrmcli-code-template-');
const json = await response.json();
const names = json.items.map((value) => ({
url: value.svn_url,
branch: value.default_branch,
name: value.name.replace('xrmcli-code-template-', ''),
}));
const repo = names.find((value) => value.name === template);
if (!repo || !repo.url || !repo.name || !repo.branch) {
console.log(`Template '${template}' not found!`);
return;
}
const downloadsource = `${repo === null || repo === void 0 ? void 0 : repo.url}/archive/refs/heads/${repo === null || repo === void 0 ? void 0 : repo.branch}.zip`;
const zipfile = await (0, node_fetch_1.default)(downloadsource);
const downloadPromise = new Promise((resolve, reject) => {
try {
zipfile.body.pipe((0, unzipper_1.Extract)({ path: `${path}/_template` })).on('close', () => {
resolve(true);
});
}
catch (err) {
console.log(`Template download failed! ${err}`);
reject(err);
}
});
const downloaded = await downloadPromise;
if (!downloaded) {
console.log('Template download failed!');
return;
}
(0, fs_extra_1.copySync)(`${path}/_template/xrmcli-code-template-${template}-main`, `${path}/`, {
recursive: true,
});
(0, fs_extra_1.removeSync)(`${path}/_template`);
const packagejson = JSON.parse((0, fs_extra_1.readFileSync)(`${path}/package.json`, { encoding: 'utf8' }));
packagejson.name = name;
(0, fs_extra_1.writeFileSync)(`${path}/package.json`, JSON.stringify(packagejson));
child_process_1.default.execSync('npm install', { stdio: 'inherit', cwd: `${path}/` });
if (xrmtypesgen) {
child_process_1.default.execSync('npm install xrmtypesgen --save-dev', {
stdio: 'inherit',
cwd: `${path}/`,
});
}
console.log('Finished.');
}
catch (err) {
console.log(`Code template command failed! ${err}`);
}
});
commander_1.program.parseAsync();
//# sourceMappingURL=create.js.map