UNPKG

chimp

Version:

Your development companion for doing quality, faster.

173 lines (172 loc) 8.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); /* eslint-disable @typescript-eslint/no-use-before-define */ const fs = tslib_1.__importStar(require("node:fs")); const path = tslib_1.__importStar(require("node:path")); const core_1 = require("@oclif/core"); const debug_1 = tslib_1.__importDefault(require("debug")); const findProjectMainPath_1 = require("../generate/helpers/findProjectMainPath"); const ListrHelper_1 = require("../generate/helpers/ListrHelper"); const assert_module_path_in_top_level_src_1 = require("../init/assert-module-path-in-top-level-src"); const assert_git_clean_state_1 = require("../init/assert-git-clean-state"); const get_chimp_version_1 = require("../helpers/get-chimp-version"); const DEFAULT_MODULES_PATH = './src/modules'; const debug = (0, debug_1.default)('commands:init'); const readJsonFile = async (path) => { if (!fs.existsSync(path)) { throw new Error(`Can't find ${path} for your project.`); } const fileContent = await fs.promises.readFile(path, { encoding: 'utf-8' }); return JSON.parse(fileContent); }; const addProjectDependencies = async (projectMainPath, modulesPath) => { function addChimp() { let chimpCommand = 'chimp generate'; // if (path.join(projectMainPath, DEFAULT_MODULES_PATH) !== path.join(projectMainPath, modulesPath)) { chimpCommand = `${chimpCommand} -p ${modulesPath}`; // } packageJsonFile.scripts.chimp = chimpCommand; packageJsonFile.devDependencies.chimp = (0, get_chimp_version_1.getChimpVersion)(); } function addDevDependenciesWithMatchingVersions(packages) { addDependencies(packages, 'devDependencies'); } function addDependenciesWithMatchingVersions(packages) { addDependencies(packages, 'dependencies'); } function addDependencies(packages, type) { for (const packageName of packages) { if (!packageJsonFile[type][packageName]) { packageJsonFile[type][packageName] = scaffoldPackageJsonFile[type][packageName]; } } } debug('reading package.json'); const packageJsonPath = path.join(projectMainPath, 'package.json'); const packageJsonFile = await readJsonFile(packageJsonPath); debug('reading scaffold package.json'); const scaffoldPackageJsonFile = await readJsonFile(path.join(__dirname, '../../scaffold/package.json')); if (!packageJsonFile.scripts) { packageJsonFile.scripts = {}; } if (!packageJsonFile.devDependencies) { packageJsonFile.devDependencies = {}; } debug('adding chimp'); addChimp(); packageJsonFile.devDependencies.prettier = scaffoldPackageJsonFile.devDependencies.prettier; packageJsonFile.devDependencies.jest = scaffoldPackageJsonFile.devDependencies.jest; debug('adding dev dependencies'); // TODO figure out remaining dependencies addDevDependenciesWithMatchingVersions([ '@types/jest', 'jest', 'prettier', 'typescript', 'ts-jest', 'testdouble', 'testdouble-jest', ]); debug('adding dependencies'); // TODO shelljs? ? addDependenciesWithMatchingVersions(['@apollo/subgraph', 'lodash', 'graphql-tag', 'tsconfig-paths']); packageJsonFile.dependencies.graphql = scaffoldPackageJsonFile.dependencies.graphql; debug('saving package.json changes'); await fs.promises.writeFile(packageJsonPath, JSON.stringify(packageJsonFile, null, 2)); // TODO prettify the file }; const createExampleCode = async (task, targetDir) => { if (fs.existsSync(targetDir)) { const exampleDir = './src/chimp-modules'; let newDir = await task.prompt({ type: 'Input', message: `${targetDir} already exists. Please enter new module name [${exampleDir}]:`, }); if (newDir === '') { newDir = exampleDir; } await createExampleCode(task, newDir); } else { const exampleModulePath = `${targetDir}/RemoveMe/graphql/`; await fs.promises.mkdir(exampleModulePath, { recursive: true }); await fs.promises.copyFile(path.join(__dirname, '../../scaffold/src/modules/removeMe/graphql/RemoveMe.graphql'), path.join(exampleModulePath, 'RemoveMe.graphql')); } }; async function configureTsconfig(projectMainPath) { const tsconfigJsonPath = path.join(projectMainPath, 'tsconfig.json'); let tsconfig; if (fs.existsSync(tsconfigJsonPath)) { tsconfig = await readJsonFile(tsconfigJsonPath); // TODO make the prefixes configurable here as well (based on the create) ? low priority. That would also require chanigng the way we generate the chimp script in add ProjectDependencies const chimpPaths = { '~app/*': ['./src/*'], '~generated/*': ['./generated/*'], }; tsconfig.compilerOptions.paths = tsconfig.compilerOptions.paths ? Object.assign(Object.assign({}, tsconfig.compilerOptions.paths), chimpPaths) : Object.assign({}, chimpPaths); if (!tsconfig.compilerOptions.baseUrl) { tsconfig.compilerOptions.baseUrl = './'; } } else { tsconfig = await readJsonFile(path.join(__dirname, '../../scaffold/tsconfig.json')); } await fs.promises.writeFile(tsconfigJsonPath, JSON.stringify(tsconfig, null, 2)); } async function configureJest(projectMainPath, task) { const jestConfigPath = path.join(projectMainPath, 'jest.config.js'); if (fs.existsSync(jestConfigPath)) { task.output = "Sorry! We can't adjust existing jest.config.js automatically yet, please take a look at https://github.com/xolvio/chimp#updating-jestconfigjs-after-chimp-init to see how to do so manually"; } else { await fs.promises.copyFile(path.join(__dirname, '../../scaffold/jest.config.js'), jestConfigPath); await fs.promises.copyFile(path.join(__dirname, '../../scaffold/jest.setup.js'), path.join(projectMainPath, 'jest.setup.js')); } } async function addContext(projectMainPath) { const contextPath = path.join(projectMainPath, 'src/context.ts'); const contextCode = `export type GqlContext = {}`; if (fs.existsSync(contextPath)) { let contextContent = await fs.promises.readFile(contextPath, { encoding: 'utf-8' }); contextContent = `${contextContent} ${contextCode}`; await fs.promises.writeFile(contextPath, contextContent); } else { await fs.promises.writeFile(contextPath, contextCode); } } class Init extends core_1.Command { async run() { (0, assert_git_clean_state_1.assertGitCleanState)(); const { flags: { modulesPath }, } = await this.parse(Init); const projectMainPath = (0, findProjectMainPath_1.findProjectMainPath)(); (0, assert_module_path_in_top_level_src_1.assertModulePathInTopLevelSrc)(projectMainPath, modulesPath); const tasks = (0, ListrHelper_1.setupListr)([ (0, ListrHelper_1.newTask)('Creating example code', (task) => createExampleCode(task, path.join(projectMainPath, modulesPath))), (0, ListrHelper_1.newTask)('Add project dependencies', () => addProjectDependencies(projectMainPath, modulesPath)), (0, ListrHelper_1.newTask)('Configure tsconfig.json', () => configureTsconfig(projectMainPath)), (0, ListrHelper_1.newTask)('Configure jest', (task) => configureJest(projectMainPath, task)), (0, ListrHelper_1.newTask)('Add empty GraphQL context file', () => addContext(projectMainPath)), // newTask('Install packages', async () => addContext(projectMainPath)), ]); // eslint-disable @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function await tasks.run().catch(() => { debug('Init failed'); }); } } Init.description = 'init Chimp'; Init.examples = ['$ chimp init', '$ chimp init -p ./src/chimp-modules']; Init.flags = { help: core_1.Flags.help({ char: 'h' }), modulesPath: core_1.Flags.string({ char: 'p', description: 'path to the GraphQL modules.', default: DEFAULT_MODULES_PATH, }), }; exports.default = Init;