@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
123 lines (121 loc) • 7.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileUpdater = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const common_1 = require("../common");
class FileUpdater {
copyNewFiles(audit, internal, updateSource, library, internalOverride) {
if (audit) {
return;
}
const ignoresPath = common_1.Common.cliRootPath + 'templates\\ignores';
const internalPath = common_1.Common.cliRootPath + 'templates\\internal';
const libraryPath = common_1.Common.cliRootPath + 'templates\\library';
const gulpPath = common_1.Common.moduleTemplatePath + 'gulpfile.ts';
const upgradedTemplatePath = common_1.Common.cliRootPath + 'templates\\wac-template';
fs_extra_1.default.copyFileSync(ignoresPath + '\\git.txt', '.\\.gitignore');
fs_extra_1.default.copyFileSync(ignoresPath + '\\npm.txt', '.\\.npmignore');
fs_extra_1.default.copyFileSync(ignoresPath + '\\browserslistrc.txt', '.\\.browserslistrc');
fs_extra_1.default.copyFileSync((internal ? internalPath : upgradedTemplatePath) + '\\.eslintrc.json', '.\\.eslintrc.json');
fs_extra_1.default.copySync(gulpPath + '\\common', '.\\gulpfile.ts\\common');
fs_extra_1.default.copyFileSync(gulpPath + '\\index.ts', '.\\gulpfile.ts\\index.ts');
fs_extra_1.default.copyFileSync(upgradedTemplatePath + '\\tsconfig.base.json', '.\\tsconfig.base.json');
fs_extra_1.default.copyFileSync(upgradedTemplatePath + '\\tsconfig.json', '.\\tsconfig.json');
fs_extra_1.default.copyFileSync((library ? libraryPath : upgradedTemplatePath) + '\\angular.json', '.\\angular.json');
fs_extra_1.default.copyFileSync(upgradedTemplatePath + '\\src\\tsconfig.app.json', '.\\src\\tsconfig.app.json');
fs_extra_1.default.copyFileSync(upgradedTemplatePath + '\\src\\tsconfig.spec.json', '.\\src\\tsconfig.spec.json');
fs_extra_1.default.copyFileSync(upgradedTemplatePath + '\\src\\polyfills.ts', '.\\src\\polyfills.ts');
fs_extra_1.default.copyFileSync(upgradedTemplatePath + '\\src\\karma.conf.js', '.\\src\\karma.conf.js');
fs_extra_1.default.copyFileSync(upgradedTemplatePath + '\\src\\test.ts', '.\\src\\test.ts');
this.updateGulpConfigFile('.\\gulpfile.ts\\config-data.ts', library);
if (internal || internalOverride) {
fs_extra_1.default.copyFileSync(internalPath + '\\version.ts', '.\\src\\version.ts');
fs_extra_1.default.copySync(internalPath + '\\azuredevops', '.\\.azuredevops');
fs_extra_1.default.copySync(common_1.Common.moduleTemplatePath + '\\build', '.\\build');
this.updateLocalizationPipeline('.\\loc\\locConfig.xml', '.\\build\\pipelines\\localization.yml', updateSource);
this.updateMainFile(internalPath + '\\main.ts', '.\\src\\main.ts');
this.updateVersionJson(internalPath + '\\version.json', '.\\version.json');
}
if (library) {
fs_extra_1.default.copyFileSync(libraryPath + '\\tsconfig.lib.json', '.\\src\\tsconfig.lib.json');
fs_extra_1.default.copyFileSync(libraryPath + '\\tsconfig.lib.prod.json', '.\\src\\tsconfig.lib.prod.json');
fs_extra_1.default.copyFileSync(libraryPath + '\\ng-package.json', '.\\src\\ng-package.json');
fs_extra_1.default.copyFileSync(libraryPath + '\\package.json', '.\\src\\package.json');
fs_extra_1.default.copyFileSync(libraryPath + '\\public_api.ts', '.\\src\\public_api.ts');
if (!fs_extra_1.default.existsSync('.\\src\\app\\index.ts')) {
fs_extra_1.default.copyFileSync(libraryPath + '\\index.ts', '.\\src\\app\\index.ts');
}
}
if (fs_extra_1.default.existsSync('.\\tsconfig.inline.json')) {
fs_1.default.unlinkSync('.\\tsconfig.inline.json');
}
if (fs_extra_1.default.existsSync('.\\index.js')) {
fs_1.default.unlinkSync('.\\index.js');
}
if (fs_extra_1.default.existsSync('.\\index.d.ts')) {
fs_1.default.unlinkSync('.\\index.d.ts');
}
console.log('All new config and json files have been transferred.');
}
updateGulpConfigFile(filePath, library) {
let fileData = common_1.Common.readFileData(filePath);
fileData = fileData.replace('\nfunction gulpConfig():', '\nexport function gulpConfig():');
fileData = fileData.replace('\nexports.gulpConfig = gulpConfig;', '');
if (library) {
if (fileData.search('build: {') === -1) {
const captureInsertLocation = /(?<=powershell: {[\s\S]+\s+)}/;
fileData = common_1.Common.replaceInString(fileData, captureInsertLocation, `},\n${' '.repeat(8)}build: {\n${' '.repeat(12)}library: true\n${' '.repeat(8)}}`);
}
else if (fileData.search('library: ') === -1) {
const captureInsertLocation = /(?<=build: ){/;
fileData = common_1.Common.replaceInString(fileData, captureInsertLocation, `{\n${' '.repeat(12)}library: true,`);
}
else {
fileData = fileData.replace('library: false', 'library: true');
}
}
fs_extra_1.default.writeFileSync(filePath, fileData);
}
updateLocalizationPipeline(locFilePath, pipelineFilePath, updateSource) {
const captureTFSSourceDirectory = /(?<=path=")%.+%\\(?=.*"\s)/;
const captureModuleId = /(?<=name=").+(?=.*"\s)/;
if (!fs_extra_1.default.existsSync(locFilePath)) {
const message = `Path ${locFilePath} does not exist. Please verify this file exists.`;
console.log(message);
updateSource.push(message + '\n');
return;
}
let locFileData = common_1.Common.readFileData(locFilePath);
locFileData = common_1.Common.replaceInString(locFileData, captureTFSSourceDirectory, '');
fs_extra_1.default.writeFileSync(locFilePath, locFileData);
const moduleId = locFileData.match(captureModuleId);
if (!moduleId || moduleId.length === 0) {
const message = `Couldn't retrieve localization module ID from ${locFilePath}, localization ID will need to be updated manually.`;
console.log(message);
updateSource.push(message + '\n');
return;
}
let pipelineFileData = common_1.Common.readFileData(pipelineFilePath);
pipelineFileData = pipelineFileData.replace('${module-id}', moduleId[0]);
fs_extra_1.default.writeFileSync(pipelineFilePath, pipelineFileData);
}
updateMainFile(internalFilePath, targetFilePath) {
let fileData = common_1.Common.readFileData(targetFilePath);
const captureName = /(?<=name: ').*(?=')/;
const name = fileData.match(captureName)[0];
fs_extra_1.default.copyFileSync(internalFilePath, targetFilePath);
fileData = common_1.Common.readFileData(targetFilePath);
fileData = fileData.replace(`{!company-name}.{!product-name}`, name);
fs_extra_1.default.writeFileSync(targetFilePath, fileData);
}
updateVersionJson(internalFilePath, targetFilePath) {
const newVersion = common_1.Common.readFileJSON(internalFilePath).version;
const versionData = common_1.Common.readFileJSON(targetFilePath);
versionData.version = newVersion;
fs_extra_1.default.writeJSONSync(targetFilePath, versionData, { spaces: 2 });
}
}
exports.FileUpdater = FileUpdater;
//# sourceMappingURL=file-updater.js.map