UNPKG

create-react-native-library

Version:
137 lines (133 loc) 4.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AVAILABLE_TOOLS = void 0; exports.configureTools = configureTools; var _fsExtra = _interopRequireDefault(require("fs-extra")); var _nodePath = _interopRequireDefault(require("node:path")); var _template = require("../template"); var _sortObjectKeys = _interopRequireDefault(require("./sortObjectKeys")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const ESLINT = { name: 'ESLint with Prettier', description: 'Lint and format code' }; const LEFTHOOK = { name: 'Lefthook with Commitlint', description: 'Manage Git hooks and lint commit messages' }; const RELEASE_IT = { name: 'Release It', description: 'Automate versioning and package publishing tasks' }; const JEST = { name: 'Jest', description: 'Test JavaScript and TypeScript code' }; const TURBOREPO = { name: 'Turborepo', description: 'Cache build outputs on CI' }; const VITE = { name: 'Vite', description: 'Add web support to the example app', condition: config => config.example != null && config.example !== 'expo', postprocess: async ({ root }) => { const examplePkgPath = _nodePath.default.join(root, 'example', 'package.json'); if (!_fsExtra.default.existsSync(examplePkgPath)) { throw new Error("Couldn't find the example app's package.json."); } // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion const examplePackageJson = await _fsExtra.default.readJson(examplePkgPath); const reactVersion = examplePackageJson.dependencies?.react ?? examplePackageJson.devDependencies?.react; if (reactVersion == null) { throw new Error("Couldn't find the package 'react' in the example app."); } examplePackageJson.dependencies = (0, _sortObjectKeys.default)({ ...examplePackageJson.dependencies, 'react-dom': reactVersion }); await _fsExtra.default.writeJson(examplePkgPath, examplePackageJson, { spaces: 2 }); } }; const AVAILABLE_TOOLS = exports.AVAILABLE_TOOLS = { eslint: ESLINT, jest: JEST, lefthook: LEFTHOOK, 'release-it': RELEASE_IT, vite: VITE }; const REQUIRED_TOOLS = { turborepo: TURBOREPO }; const ALL_TOOLS = { ...AVAILABLE_TOOLS, ...REQUIRED_TOOLS }; async function configureTools({ tools, config, root, packageJson }) { for (const key of [...tools, // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion ...Object.keys(REQUIRED_TOOLS)]) { if (!(key in ALL_TOOLS)) { throw new Error(`Invalid tool '${key}'. Available tools are: ${Object.keys(AVAILABLE_TOOLS).join(', ')}.`); } // @ts-expect-error: We checked the key above const tool = ALL_TOOLS[key]; if (tool.condition && !tool.condition(config)) { continue; } const toolDir = _nodePath.default.resolve(__dirname, `../../templates/tools/${key}`); if (_fsExtra.default.existsSync(toolDir)) { await (0, _template.applyTemplate)(config, toolDir, root); } const examplePkgPath = _nodePath.default.join(toolDir, 'example', '~package.json'); await mergePackageJsonTemplate(_nodePath.default.join(toolDir, '~package.json'), packageJson); if (_fsExtra.default.existsSync(examplePkgPath)) { // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion const existingExamplePackageJson = await _fsExtra.default.readJson(_nodePath.default.join(root, 'example', 'package.json')); await mergePackageJsonTemplate(examplePkgPath, existingExamplePackageJson); await _fsExtra.default.writeJson(_nodePath.default.join(root, 'example', 'package.json'), existingExamplePackageJson, { spaces: 2 }); } await tool.postprocess?.({ config, root }); } } async function mergePackageJsonTemplate(templatePath, packageJson) { if (!_fsExtra.default.existsSync(templatePath)) { return; } // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion const template = await _fsExtra.default.readJson(templatePath); for (const [field, value] of Object.entries(template)) { if (typeof value === 'object' && value !== null && !Array.isArray(value)) { if (typeof packageJson[field] === 'object' || packageJson[field] == null) { packageJson[field] = { ...packageJson[field], ...value }; if (field === 'dependencies' || field === 'devDependencies' || field === 'peerDependencies') { packageJson[field] = (0, _sortObjectKeys.default)(packageJson[field]); } } else { throw new Error(`Cannot merge '${field}' field because it is not an object (got '${String(packageJson[field])}').`); } } else { packageJson[field] = value; } } } //# sourceMappingURL=configureTools.js.map