@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
184 lines (182 loc) • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtensionCreator = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const guid_typescript_1 = require("guid-typescript");
const path_exists_1 = tslib_1.__importDefault(require("path-exists"));
const common_1 = require("../common");
const update_shell_references_1 = require("../angular15/utils/update-shell-references");
class ExtensionCreator {
argv;
/**
* Arguments:
* --company - name of the company or person making the extension.
* --tool - name of the tool / extension that is being built.
* --solution - name of the solution / extension that is being built.
* --version - tag of the version to use.
* --internal - instead of referring to SDK, references point to internal shell library
* @param {any} argv Object containing command arguments
*/
constructor(argv) {
this.argv = argv;
}
create() {
const normalizedCompany = this.normalizeString(this.argv.company ? this.argv.company : '');
const normalizedTool = this.normalizeString(this.argv.tool ? this.argv.tool : '');
const version = this.argv.version ? this.argv.version.toLowerCase() : '';
const extensionType = this.argv.solution ? 'solution' : 'tool';
const normalizedSolution = this.argv.solution ? this.normalizeString(this.argv.solution) : '';
const internal = this.argv.internal;
if (this.argv.length === 0 || !this.isValidVersion(version) || !normalizedCompany || !normalizedTool) {
console.error('Usage: wac create --company <company-name> --name <tool-name> --version <version-tag> [--verbose]');
console.log('or');
console.log('wac create --company <company-name> --solution <solution-name> --tool <tool-name> --type <tool-type> --version <version-tag> [--verbose]');
console.log('Valid version tags: \'latest\', \'insider\', \'next\', \'experimental\'');
console.log('More information can be found here:');
process.exit(1);
}
if (normalizedSolution === '') {
this.createHelper(extensionType, normalizedCompany, normalizedTool, '', version, internal);
}
else {
this.createHelper(extensionType, normalizedCompany, normalizedSolution, normalizedTool, version, internal);
}
}
createHelper(type, company, primaryDisplayName, secondaryDisplayName, version, internal) {
const ignoresPath = common_1.Common.cliRootPath + 'templates\\ignores';
const internalPath = common_1.Common.cliRootPath + 'templates\\internal';
const solutionPath = common_1.Common.cliRootPath + 'templates\\solution';
const templatePath = common_1.Common.cliRootPath + 'templates\\wac-template';
const gulpPath = common_1.Common.moduleTemplatePath;
if (path_exists_1.default.sync(primaryDisplayName)) {
console.error('This tool definition already exists. No changes have been made.');
}
else {
const productPath = './' + primaryDisplayName;
fs_extra_1.default.mkdirSync(primaryDisplayName);
console.log('Created ' + productPath);
fs_extra_1.default.copySync(templatePath, productPath);
fs_extra_1.default.copySync(gulpPath + '\\gulpfile.ts', productPath + '\\gulpfile.ts');
fs_extra_1.default.copySync(gulpPath + '\\build', productPath + '\\build');
fs_extra_1.default.copyFileSync(gulpPath + '\\.eslintrc.json', productPath + '\\.eslintrc.json');
fs_extra_1.default.copyFileSync(templatePath + '\\gulpfile.ts\\config-data.ts', productPath + '\\gulpfile.ts\\config-data.ts');
fs_extra_1.default.copyFileSync(ignoresPath + '\\git.txt', productPath + '\\.gitignore');
fs_extra_1.default.copyFileSync(ignoresPath + '\\npm.txt', productPath + '\\.npmignore');
fs_extra_1.default.copyFileSync(ignoresPath + '\\browserslistrc.txt', productPath + '\\.browserslistrc');
// For solution extensions, overwrite manifest and strings file
if (type === 'solution') {
fs_extra_1.default.copyFileSync(solutionPath + '\\manifest.json', productPath + '\\src\\manifest.json');
fs_extra_1.default.copyFileSync(solutionPath + '\\strings.resjson', productPath + '\\src\\resources\\strings\\strings.resjson');
}
// For internal developers, overwrite package.json and add .npmrc
if (internal) {
fs_extra_1.default.copyFileSync(internalPath + '\\package.json', productPath + '\\package.json');
fs_extra_1.default.copyFileSync(internalPath + '\\npmrc.txt', productPath + '\\.npmrc');
}
this.updateShellReferences(primaryDisplayName, internal);
this.updateFiles(company, primaryDisplayName, secondaryDisplayName, version, internal);
this.printOutro(primaryDisplayName);
}
}
updateShellReferences(primaryDisplayName, internal) {
(0, update_shell_references_1.updateShellReferences)(primaryDisplayName, internal);
}
updateFiles(company, primaryDisplayName, secondaryDisplayName, version, internal) {
/*
/ files that need updating:
/ root package.json
/ src/manifest.json
/ src/resources/strings/strings.resjson
/ src/main.ts
/ in Experimental: gulpfile.js
*/
const cleanDirectory = {};
const rootPackagePath = './' + primaryDisplayName + '/package.json';
const manifestFilePath = './' + primaryDisplayName + '/src/manifest.json';
const mainFilePath = './' + primaryDisplayName + '/src/main.ts';
const stringsFilePath = './' + primaryDisplayName + '/src/resources/strings/strings.resjson';
const gulpFilePath = './' + primaryDisplayName + '/gulpfile.ts/config-data.ts';
const packageName = '@' + company.toLowerCase() + '/' + primaryDisplayName.toLowerCase();
const manifestName = company.toLowerCase() + '.' + primaryDisplayName.toLowerCase();
const stringsProduct = primaryDisplayName.split('-').join(''); // Strings file cannot handle dashes.
const stringsCompany = company.split('-').join('');
const companyPackageIdentifier = company.split('-').join('') + primaryDisplayName.split('-').join('');
/*
/ Default version is 'latest' in wac-template/package.json
*/
if (version === 'next' || version === 'insider' || version === 'experimental') {
const existingVersion = '"@microsoft/windows-admin-center-sdk": "latest",';
cleanDirectory[rootPackagePath] = {
'@{!company-name}/{!product-name}': packageName,
'"@microsoft/windows-admin-center-sdk": "latest",': existingVersion.replace('latest', version)
};
}
else {
cleanDirectory[rootPackagePath] = { '@{!company-name}/{!product-name}': packageName };
}
if (!internal) {
this.cleanInternalPackages(rootPackagePath);
}
cleanDirectory[gulpFilePath] = {
'{!company-name}.{!module-name}': manifestName,
'{!CompanyName}{!ProductName}': stringsCompany + stringsProduct,
'{!guid}': guid_typescript_1.Guid.create(),
'{!company-package-id}': companyPackageIdentifier
};
cleanDirectory[manifestFilePath] = {
'{!company-name}.{!module-name}': manifestName,
'{!company-name}.{!product-name}': manifestName,
'{!primary-display-name}': primaryDisplayName,
'{!primary-url-name}': primaryDisplayName.toLowerCase(),
'{!secondary-display-name}': secondaryDisplayName,
'{!secondary-url-name}': secondaryDisplayName.toLowerCase()
};
cleanDirectory[stringsFilePath] = {
'{!product-display-name}': stringsProduct,
'{!product-title}': stringsProduct,
'{!primary-display-name}': primaryDisplayName,
'{!secondary-display-name}': secondaryDisplayName,
'{!ProductName}': stringsProduct,
'{!CompanyName}': stringsCompany
};
cleanDirectory[mainFilePath] = { '{!company-name}.{!product-name}': manifestName };
for (const key in cleanDirectory) {
if (Object.prototype.hasOwnProperty.call(cleanDirectory, key)) {
this.cleanFile(key, cleanDirectory[key]);
}
}
}
cleanFile(key, values) {
(0, update_shell_references_1.cleanFile)(key, values);
}
cleanInternalPackages(packagesFilePath) {
const packages = common_1.Common.readFileJSON(packagesFilePath);
const dependencies = [packages.peerDependencies, packages.devDependencies];
const shellPackageFile = common_1.Common.cliRootPath + 'src\\config\\shell-package-list.json';
const shellPackages = common_1.Common.readFileJSON(shellPackageFile).packages;
dependencies.forEach((target) => {
for (const pack of shellPackages) {
delete target[pack];
}
});
fs_extra_1.default.writeJSONSync(packagesFilePath, packages, { spaces: 2 });
}
printOutro(product) {
console.log('');
console.log('Thank you for using the Windows Admin Center CLI.');
console.log('');
console.log('Next steps:');
console.log('cd into your new directory (cd ' + product + ') and then run \'npm install\' -> \'gulp build\' to build your new extension');
console.log('After that, \'gulp serve --port 4200\' to serve your new extension on port 4200');
console.log('Additional information can be found here: https://aka.ms/wacsdkdocs');
}
normalizeString(input) {
return input.split(' ').join('-');
}
isValidVersion(version) {
return version === 'latest' || version === 'next' || version === 'insider' || version === 'release' || version === '' || version === 'experimental';
}
}
exports.ExtensionCreator = ExtensionCreator;
//# sourceMappingURL=main.js.map