PHP WebShell
Текущая директория: /opt/BitGoJS/node_modules/nx/src/nx-init
Просмотр файла: add-nx-to-nest.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addNxToNest = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = require("fs-extra");
const yargsParser = require("yargs-parser");
const enquirer = require("enquirer");
const path_1 = require("path");
const output_1 = require("../utils/output");
const fileutils_1 = require("../utils/fileutils");
const utils_1 = require("./utils");
const package_manager_1 = require("../utils/package-manager");
const add_nx_to_npm_repo_1 = require("./add-nx-to-npm-repo");
const parsedArgs = yargsParser(process.argv, {
boolean: ['yes'],
string: ['cacheable'],
alias: {
yes: ['y'],
},
});
function addNxToNest(packageJson) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const repoRoot = process.cwd();
output_1.output.log({ title: `🐳 Nx initialization` });
// we check upstream that nest-cli.json exists before it reaches this function
// so it is guaranteed to be here
const nestCliJson = (0, fileutils_1.readJsonFile)((0, path_1.join)(repoRoot, 'nest-cli.json'));
const nestCLIConfiguration = mergeWithDefaultConfig(nestCliJson);
// For NestJS CLI Monorepo, this property is always "true"
if (nestCLIConfiguration.monorepo) {
// TODO: update message for NestJS CLI Monorepo support
output_1.output.log({ title: 'NestCLI Monorepo support is coming soon' });
return;
}
const isJS = nestCLIConfiguration.language === 'js';
const nestCacheableScripts = ['build', 'lint', 'test'];
const nestIgnoreScripts = [
'start',
'start:dev',
'start:debug',
'test:cov',
'test:watch',
];
const scripts = Object.keys(packageJson.scripts).filter((s) => {
if (nestCacheableScripts.includes(s) || nestIgnoreScripts.includes(s)) {
return false;
}
return !s.startsWith('pre') && !s.startsWith('post');
});
let cacheableOperations;
let scriptOutputs = {};
let useCloud;
if (parsedArgs.yes !== true) {
output_1.output.log({
title: `🧑🔧 Please answer the following questions about the scripts found in your package.json in order to generate task runner configuration`,
});
cacheableOperations = (yield enquirer.prompt([
{
type: 'multiselect',
name: 'cacheableOperations',
message: 'Which of the following scripts are cacheable? (Produce the same output given the same input, e.g. build, test and lint usually are, serve and start are not)',
choices: scripts,
},
])).cacheableOperations;
for (const scriptName of cacheableOperations) {
scriptOutputs[scriptName] = (yield enquirer.prompt([
{
type: 'input',
name: scriptName,
message: `Does the "${scriptName}" script create any outputs? If not, leave blank, otherwise provide a path (e.g. dist, lib, build, coverage)`,
},
]))[scriptName];
}
useCloud = yield (0, utils_1.askAboutNxCloud)();
}
else {
cacheableOperations = parsedArgs.cacheable
? parsedArgs.cacheable.split(',')
: [];
useCloud = false;
}
(0, utils_1.createNxJsonFile)(repoRoot, [], [...cacheableOperations, ...nestCacheableScripts], {});
const pmc = (0, package_manager_1.getPackageManagerCommand)();
(0, utils_1.addDepsToPackageJson)(repoRoot, useCloud);
addNestPluginToPackageJson(repoRoot);
(0, add_nx_to_npm_repo_1.markRootPackageJsonAsNxProject)(repoRoot, cacheableOperations, scriptOutputs, pmc);
createProjectJson(repoRoot, packageJson, nestCLIConfiguration);
removeFile(repoRoot, 'nest-cli.json');
updatePackageJsonScripts(repoRoot, isJS);
if (!isJS) {
updateTsConfig(repoRoot, nestCLIConfiguration.sourceRoot);
}
output_1.output.log({ title: `📦 Installing dependencies` });
(0, utils_1.runInstall)(repoRoot);
if (useCloud) {
(0, utils_1.initCloud)(repoRoot, 'nx-init-nest');
}
printFinalMessage();
});
}
exports.addNxToNest = addNxToNest;
function printFinalMessage() {
output_1.output.success({
title: `🎉 Done!`,
bodyLines: [
`- Enabled computation caching!`,
`- Learn more at https://nx.dev/recipes/adopting-nx/adding-to-monorepo`,
],
});
}
function addNestPluginToPackageJson(repoRoot) {
const path = (0, path_1.join)(repoRoot, `package.json`);
const json = (0, fileutils_1.readJsonFile)(path);
json.devDependencies['@nrwl/nest'] = require('../../package.json').version;
json.devDependencies['@nrwl/jest'] = require('../../package.json').version;
(0, fileutils_1.writeJsonFile)(path, json);
}
function createProjectJson(repoRoot, packageJson, nestCLIOptions) {
const packageName = packageJson.name;
const path = (0, path_1.join)(repoRoot, 'project.json');
const json = {
name: packageName,
root: '.',
sourceRoot: nestCLIOptions.sourceRoot,
projectType: 'application',
targets: {},
tags: [],
};
json['$schema'] = 'node_modules/nx/schemas/project-schema.json';
if (nestCLIOptions.language !== 'js') {
json.targets['serve'] = {
executor: '@nrwl/js:node',
options: {
buildTarget: `${packageName}:build`,
},
};
console.log(nestCLIOptions);
if (nestCLIOptions.webpackOptions) {
json.targets['build'] = {
executor: '@nrwl/webpack:webpack',
outputs: ['{options.outputPath}'],
options: {
target: 'node',
compiler: 'tsc',
outputPath: `dist/${packageName}`,
main: (0, path_1.join)(nestCLIOptions.sourceRoot, nestCLIOptions.entryFile),
tsConfig: 'tsconfig.build.json',
},
configurations: {
production: {
optimization: true,
extractLicenses: true,
inspect: false,
},
},
};
json.targets['serve'] = Object.assign(Object.assign({}, json.targets['serve']), { configurations: {
production: {
buildTarget: `${packageName}:build:production`,
},
} });
}
else {
json.targets['build'] = {
executor: '@nrwl/js:tsc',
outputs: ['{options.outputPath}'],
options: {
outputPath: `dist/${packageName}`,
main: (0, path_1.join)(nestCLIOptions.sourceRoot, nestCLIOptions.entryFile),
tsConfig: 'tsconfig.build.json',
},
};
json.targets['serve'] = Object.assign(Object.assign({}, json.targets['serve']), { configurations: {
debug: {
inspect: 'inspect',
},
} });
// if we're using nrwl/js, then we add nrwl/js analyzeSourceFiles to nx.json
addNrwlJsPluginsConfig(repoRoot);
}
// lint
json.targets['lint'] = {
executor: '@nrwl/linter:eslint',
outputs: ['{options.outputFile}'],
options: {
lintFilePatterns: ['src/**/*.ts', 'test/**/*.ts'],
},
};
// test and e2e
addJestTargets(repoRoot, packageName, json, packageJson);
}
(0, fileutils_1.writeJsonFile)(path, json);
}
function getJestOptions(isE2E, repoRoot, packageName, existingOptions) {
// try get the e2e json if we find it
if (isE2E && !existingOptions) {
try {
existingOptions = (0, fileutils_1.readJsonFile)((0, path_1.join)(repoRoot, 'test/jest-e2e.json'));
removeFile(repoRoot, 'test/jest-e2e.json');
}
catch (e) { }
}
const jestOptions = existingOptions || {
moduleFileExtensions: ['js', 'json', 'ts'],
testEnvironment: 'node',
transform: { '^.+\\.(t|j)s$': 'ts-jest' },
};
jestOptions['displayName'] = isE2E ? `${packageName}-e2e` : packageName;
// remove rootDir and testRegex, we'll use testMatch instead since we'll have the
// root jest.preset.js in the root instead of 'src'
delete jestOptions['rootDir'];
delete jestOptions['testRegex'];
jestOptions['testMatch'] = isE2E
? ['<rootDir>/test/**/?(*.)+(e2e-spec|e2e-test).[jt]s?(x)']
: ['<rootDir>/src/**/*(*.)@(spec|test).[jt]s?(x)'];
// set coverage directory for unit test
if (!isE2E) {
jestOptions['coverageDirectory'] = `./coverage/${packageName}`;
}
return jestOptions;
}
function tryCreateJestPreset(repoRoot) {
const jestPresetPath = (0, path_1.join)(repoRoot, 'jest.preset.js');
if (!(0, fileutils_1.fileExists)(jestPresetPath)) {
(0, fs_extra_1.writeFileSync)(jestPresetPath, `
const nxPreset = require('@nrwl/jest/preset').default;
module.exports = {...nxPreset};
`, 'utf8');
return true;
}
return false;
}
function addJestTargets(repoRoot, packageName, projectJson, packageJson) {
const unitTestOptions = getJestOptions(false, repoRoot, packageName, packageJson['jest']);
const unitTestConfigPath = 'jest.config.ts';
const e2eTestOptions = getJestOptions(true, repoRoot, packageName);
const e2eTestConfigPath = 'jest.e2e-config.ts';
const isPresetCreated = tryCreateJestPreset(repoRoot);
if (isPresetCreated) {
unitTestOptions['preset'] = e2eTestOptions['preset'] = './jest.preset.js';
}
(0, fs_extra_1.writeFileSync)(unitTestConfigPath, `export default ${JSON.stringify(unitTestOptions, null, 2)}`, 'utf8');
(0, fs_extra_1.writeFileSync)(e2eTestConfigPath, `export default ${JSON.stringify(e2eTestOptions, null, 2)}`, 'utf8');
projectJson.targets['test'] = {
executor: '@nrwl/jest:jest',
outputs: [`{workspaceRoot}/coverage/${packageName}`],
options: {
passWithNoTests: true,
jestConfig: unitTestConfigPath,
},
};
projectJson.targets['e2e'] = {
executor: '@nrwl/jest:jest',
options: {
passWithNoTests: true,
jestConfig: e2eTestConfigPath,
},
};
// remove jest options from package.json
delete packageJson['jest'];
}
function addNrwlJsPluginsConfig(repoRoot) {
const path = (0, path_1.join)(repoRoot, 'nx.json');
const json = (0, fileutils_1.readJsonFile)(path);
if (!json.pluginsConfig) {
json.pluginsConfig = {
'@nrwl/js': {
analyzeSourceFiles: true,
},
};
}
(0, fileutils_1.writeJsonFile)(path, json);
}
function updatePackageJsonScripts(repoRoot, isJS) {
const path = (0, path_1.join)(repoRoot, `package.json`);
const json = (0, fileutils_1.readJsonFile)(path);
if (json.scripts['build']) {
json.scripts['build'] = 'nx build';
}
if (json.scripts['lint']) {
json.scripts['lint'] = 'nx lint';
}
if (json.scripts['start:debug']) {
json.scripts['start:debug'] = 'nx serve --configuration=debug';
}
if (json.scripts['test']) {
json.scripts['test'] = 'nx test';
}
if (json.scripts['test:cov']) {
delete json.scripts['test:cov'];
}
if (json.scripts['test:watch']) {
delete json.scripts['test:watch'];
}
if (json.scripts['test:e2e']) {
delete json.scripts['test:e2e'];
json.scripts['e2e'] = 'nx e2e';
}
if (!isJS) {
if (json.scripts['start']) {
json.scripts['start'] = 'nx serve';
}
if (json.scripts['start:dev']) {
// same as nx serve
delete json.scripts['start:dev'];
}
}
(0, fileutils_1.writeJsonFile)(path, json);
}
function updateTsConfig(repoRoot, sourceRoot) {
const path = (0, path_1.join)(repoRoot, `tsconfig.build.json`);
const json = (0, fileutils_1.readJsonFile)(path);
// we add include to the tsconfig.build because our executor runs tsc with
// generated tsconfig which is in `tmp/**.generated.json`. By default, tsc
// cannot find the default source files anymore.
if (!json.include)
json.include = [];
json.include.push(`${sourceRoot}/**/*.ts`);
(0, fileutils_1.writeJsonFile)(path, json);
}
function removeFile(repoRoot, file) {
const path = (0, path_1.join)(repoRoot, file);
(0, fs_extra_1.unlinkSync)(path);
}
function mergeWithDefaultConfig(config) {
const defaultNestCliConfigurations = {
language: 'ts',
sourceRoot: 'src',
collection: '@nestjs/schematics',
entryFile: 'main',
projects: {},
monorepo: false,
compilerOptions: {
tsConfigPath: 'tsconfig.build.json',
webpack: false,
webpackConfigPath: 'webpack.config.js',
plugins: [],
assets: [],
},
generateOptions: {},
};
if (config.compilerOptions) {
return Object.assign(Object.assign(Object.assign({}, defaultNestCliConfigurations), config), { compilerOptions: Object.assign(Object.assign({}, defaultNestCliConfigurations.compilerOptions), config.compilerOptions) });
}
return Object.assign(Object.assign({}, defaultNestCliConfigurations), config);
}
//# sourceMappingURL=add-nx-to-nest.js.mapВыполнить команду
Для локальной разработки. Не используйте в интернете!