@o3r/pipeline
Version:
A package that provides toolchain related helpers.
96 lines • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ngAdd = void 0;
const fs = require("node:fs");
const path = require("node:path");
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@o3r/schematics");
const js_yaml_1 = require("js-yaml");
/**
* Determines if the Yarn version is 2 or higher based on the contents of the .yarnrc.yml file.
* @param tree tree
*/
function isYarn2(tree) {
const yarnrcPath = '/.yarnrc.yml';
if (tree.exists(yarnrcPath)) {
const { yarnPath } = ((0, js_yaml_1.load)(tree.readText(yarnrcPath)) || {});
return !yarnPath || !/yarn-1\./.test(yarnPath);
}
return false;
}
/**
* Add an Otter CI pipeline to an Angular Project
* @param options
*/
function ngAddFn(options) {
return (tree, context) => {
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const ownPackageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' }));
const commitHash = ownPackageJson.config?.o3r?.commitHash;
const ownVersion = ownPackageJson.version;
const actionVersionString = commitHash ? `${commitHash} # v${ownVersion}` : `v${ownVersion}`;
let packageManager = 'npm';
try {
packageManager = (0, schematics_2.getPackageManager)({ workspaceConfig: (0, schematics_2.getWorkspaceConfig)(tree) });
}
catch {
packageManager = tree.exists('/yarn.lock') ? 'yarn' : 'npm';
}
context.logger.info(`Setting up pipeline for package manager: "${packageManager}" `);
const setupCommand = packageManager === 'yarn' ? 'yarn install --immutable' : 'npm ci';
const yarn2 = packageManager === 'yarn' && isYarn2(tree);
const baseTemplateSource = (0, schematics_1.apply)((0, schematics_1.url)(`./templates/${options.toolchain}`), [
(0, schematics_1.template)({
...options,
npmRegistry: options.npmRegistry || '',
defaultBranchName: options.defaultBranchName || 'main',
packageManager,
setupCommand,
actionVersionString,
yarn2,
dot: '.'
}),
(0, schematics_1.move)(tree.root.path)
]);
const npmRegistryRule = () => {
if (!options.npmRegistry) {
return tree;
}
if (yarn2) {
const yarnrcPath = '/.yarnrc.yml';
const yarnrcContent = (0, js_yaml_1.load)(tree.readText(yarnrcPath));
yarnrcContent.npmRegistryServer = options.npmRegistry;
tree.overwrite(yarnrcPath, (0, js_yaml_1.dump)(yarnrcContent, { indent: 2 }));
}
else {
// both npm and yarn 1 use .npmrc for the registry
const npmrcPath = '/.npmrc';
if (tree.exists(npmrcPath)) {
const npmrcContent = tree.readText(npmrcPath);
const registryPattern = /^registry=.*$/m;
const newRegistryLine = `registry=${options.npmRegistry}`;
const newContent = registryPattern.test(npmrcContent)
? npmrcContent.replace(registryPattern, newRegistryLine)
: `${npmrcContent}\n${newRegistryLine}`;
tree.overwrite(npmrcPath, newContent);
}
else {
tree.create(npmrcPath, `registry=${options.npmRegistry}`);
}
}
return tree;
};
const rules = [
(0, schematics_1.mergeWith)(baseTemplateSource, schematics_1.MergeStrategy.Overwrite),
npmRegistryRule
];
return () => (0, schematics_1.chain)(rules)(tree, context);
};
}
/**
* Add an Otter CI pipeline to an Angular project
* @param options
*/
const ngAdd = (options) => (0, schematics_2.createOtterSchematic)(ngAddFn)(options);
exports.ngAdd = ngAdd;
//# sourceMappingURL=index.js.map