templates-mo
Version:
Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting
141 lines (140 loc) • 5.07 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.packageManagers = exports.runCommand = exports.linters = exports.runFormatter = exports.FORMATTER_PROMPT = void 0;
/**
* Inspired from hey-api/openapi-ts
* https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/index.ts
*/
const cross_spawn_1 = require("cross-spawn");
const path_1 = __importDefault(require("path"));
const fileSystem_1 = require("../utilities/fileSystem");
const NONE = 'none';
function isErrnoException(error) {
return error.code !== undefined;
}
exports.FORMATTER_PROMPT = {
name: 'formatter',
aliases: ['format'],
description: 'Type of formatter you would like to use to format the component',
message: 'What type of formatter do you want to use to format your code',
type: 'list',
tpsType: 'data',
hidden: true,
choices: [NONE, 'prettier', 'biome'],
default: NONE,
};
/**
* Map of supported formatters
*/
const formattersMap = {
biome: {
args: (paths) => ['format', '--write', ...paths],
command: 'biome',
name: 'Biome (Format)',
},
prettier: {
args: (paths) => [
'--ignore-unknown',
...paths,
'--write',
'--ignore-path',
'./.prettierignore',
],
command: 'prettier',
name: 'Prettier',
},
};
const runFormatter = (formatter, cwd, paths, tps) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
if (formatter === NONE) {
return;
}
const formatterObj = (_a = formattersMap[formatter]) !== null && _a !== void 0 ? _a : null;
if (formatterObj) {
(0, exports.runCommand)(formatterObj, cwd, paths, tps);
}
});
exports.runFormatter = runFormatter;
/**
* Map of supported linters
*/
exports.linters = {
biome: {
args: (paths) => ['lint', '--apply', ...paths],
command: 'biome',
name: 'Biome (Lint)',
},
eslint: {
args: (paths) => [...paths, '--fix'],
command: 'eslint',
name: 'ESLint',
},
};
const runCommand = (module, cwd, paths, tps = null) => {
console.log(`✨ Running ${module.name}`);
const templatesNodeModulesBin = path_1.default.resolve(__dirname, '../../', 'node_modules', '.bin');
// TODO: Use find up
// const destNodeModulesBin = path.resolve(cwd, 'node_modules', '.bin');
const parentNodeModules = (0, fileSystem_1.findUp)('node_modules', cwd);
const destNodeModulesBin = parentNodeModules
? [path_1.default.resolve(parentNodeModules, '.bin')]
: [];
const templateNodeModuleBin = tps
? [path_1.default.resolve(tps.src, 'node_modules', '.bin')]
: [];
const bins = [
// Bins from main template directory which satisfies default templates
templatesNodeModulesBin,
// Bins from Modules from the `cwd` or in other words `dest`
...destNodeModulesBin,
// Bins from template modules
...templateNodeModuleBin,
process.env.PATH,
];
const envPath = bins.join(':');
const result = (0, cross_spawn_1.sync)(module.command, module.args(paths), {
env: Object.assign(Object.assign({}, process.env), { PATH: envPath }),
});
if (result === null || result === void 0 ? void 0 : result.error) {
const { error } = result;
if (isErrnoException(error)) {
if (error.code === 'ENOENT') {
console.log(`❌ Command not found: ${module.command}`);
return;
}
}
console.log(`❌ ${module.name} failed!`);
console.log(error);
}
else if ((result === null || result === void 0 ? void 0 : result.status) > 0) {
// command error
console.log(`❌ ${module.name} failed!`);
console.log(result.stdout.toString());
console.log(result.stderr.toString());
}
};
exports.runCommand = runCommand;
exports.packageManagers = {
npm: {
args: (paths) => ['install', '--prefix', ...paths],
command: 'npm',
name: 'Npm Install',
},
yarn: {
args: (paths) => ['install', '--cwd', ...paths],
command: 'yarn',
name: 'Yarn Install',
},
};