@nestjs/cli
Version:
The Nest CLI: create NestJS applications, generate modules, controllers and CRUD resources, and build, run and watch projects and monorepos.
91 lines (90 loc) • 4.49 kB
JavaScript
import { fork } from 'child_process';
import { dirname, join, normalize, relative } from 'path';
import { fileURLToPath } from 'url';
import { treeKillSync } from '../utils/tree-kill.js';
import { getValueOrDefault } from './helpers/get-value-or-default.js';
import { PluginMetadataGenerator } from './plugins/plugin-metadata-generator.js';
import { FOUND_NO_ISSUES_GENERATING_METADATA, FOUND_NO_ISSUES_METADATA_GENERATION_SKIPPED, } from './swc/constants.js';
import { TypeCheckerHost } from './swc/type-checker-host.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export class BaseCompiler {
pluginsLoader;
pluginMetadataGenerator = new PluginMetadataGenerator();
typeCheckerHost = new TypeCheckerHost();
constructor(pluginsLoader) {
this.pluginsLoader = pluginsLoader;
}
loadPlugins(configuration, tsConfigPath, appName) {
const pluginsConfig = getValueOrDefault(configuration, 'compilerOptions.plugins', appName);
const pathToSource = this.getPathToSource(configuration, tsConfigPath, appName);
const plugins = this.pluginsLoader.load(pluginsConfig, { pathToSource });
return plugins;
}
getPathToSource(configuration, tsConfigPath, appName) {
const sourceRoot = getValueOrDefault(configuration, 'sourceRoot', appName, 'sourceRoot');
const cwd = process.cwd();
const relativeRootPath = dirname(relative(cwd, tsConfigPath));
const pathToSource = normalize(sourceRoot).indexOf(normalize(relativeRootPath)) >= 0
? join(cwd, sourceRoot)
: join(cwd, relativeRootPath, sourceRoot);
return pathToSource;
}
runTypeChecker(configuration, tsConfigPath, appName, extras) {
if (extras.watch) {
const args = [
tsConfigPath,
appName ?? 'undefined',
// The forked checker resolves its plugin metadata output directory
// from this value, so it has to be the built application's source
// root, not the monorepo's default project one.
getValueOrDefault(configuration, 'sourceRoot', appName, 'sourceRoot') ?? 'src',
// Same for the plugins: a monorepo application can declare its own
// "compilerOptions.plugins", and the non-watch path loads them per
// application through loadPlugins().
JSON.stringify(getValueOrDefault(configuration, 'compilerOptions.plugins', appName) ?? []),
];
const childProcessRef = fork(join(__dirname, 'swc/forked-type-checker.js'), args, {
cwd: process.cwd(),
});
process.on('exit', () => childProcessRef && treeKillSync(childProcessRef.pid));
}
else {
const { readonlyVisitors } = this.loadPlugins(configuration, tsConfigPath, appName);
const outputDir = this.getPathToSource(configuration, tsConfigPath, appName);
let fulfilled = false;
return new Promise((resolve, reject) => {
try {
this.typeCheckerHost.run(tsConfigPath, {
watch: extras.watch,
onProgramInit: readonlyVisitors.length > 0
? (program) => this.pluginMetadataGenerator.generate({
outputDir,
visitors: readonlyVisitors,
tsProgramRef: program,
})
: undefined,
onTypeCheck: () => {
if (!fulfilled) {
fulfilled = true;
resolve();
}
if (readonlyVisitors.length > 0) {
process.nextTick(() => console.log(FOUND_NO_ISSUES_GENERATING_METADATA));
}
else {
process.nextTick(() => console.log(FOUND_NO_ISSUES_METADATA_GENERATION_SKIPPED));
}
},
});
}
catch (err) {
if (!fulfilled) {
fulfilled = true;
reject(err);
}
}
});
}
}
}