chimp
Version:
Your development companion for doing quality, faster.
106 lines (105 loc) • 6 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("node:fs"));
const path = tslib_1.__importStar(require("node:path"));
const core_1 = require("@oclif/core");
const shelljs_1 = tslib_1.__importDefault(require("shelljs"));
const execQuietly_1 = require("../generate/helpers/execQuietly");
const findProjectMainPath_1 = require("../generate/helpers/findProjectMainPath");
const ListrHelper_1 = require("../generate/helpers/ListrHelper");
const assert_git_clean_state_1 = require("../init/assert-git-clean-state");
function installingTooling(projectMainPath) {
function determinePackageManager() {
if (fs.existsSync('./yarn.lock')) {
return 'yarn';
}
if (fs.existsSync('./pnpm-lock.yaml')) {
return 'pnpm';
}
return 'npm';
}
function installPackage(packageManager, packageName) {
let command;
switch (packageManager) {
case 'yarn':
command = `yarn add --dev ${packageName}`;
break;
case 'pnpm':
command = `pnpm add --save-dev ${packageName}`;
break;
case 'npm':
default:
command = `npm install --save-dev ${packageName}`;
break;
}
console.log('installing packages');
return (0, execQuietly_1.execQuietly)(command, { cwd: projectMainPath });
}
const packageManager = determinePackageManager();
return installPackage(packageManager, '@graphql-tools/graphql-file-loader @graphql-tools/load @graphql-tools/merge');
}
function moveCode(projectMainPath, chimpMainPath) {
shelljs_1.default.cp(path.join(chimpMainPath, 'src/generate/templates/ejectedSchema.ts'), path.join(projectMainPath, 'src/schema.ts'));
// Move genericDataModelSchema.graphql
shelljs_1.default.mv(path.join(projectMainPath, 'generated/graphql/genericDataModelSchema.graphql'), path.join(projectMainPath, 'src/genericDataModelSchema.graphql'));
// Move types.ts
shelljs_1.default.mv(path.join(projectMainPath, 'generated/graphql/types.ts'), path.join(projectMainPath, 'src/graphqlTypes.ts'));
for (const resolverFile of shelljs_1.default.ls(path.join(projectMainPath, 'generated/graphql/*Resolvers.ts'))) {
const fileName = resolverFile.split('/').pop();
const moduleName = fileName.replace('Resolvers.ts', '');
const targetDir = path.join(projectMainPath, `src/modules/${moduleName}/graphql/`);
shelljs_1.default.mv(resolverFile, targetDir);
}
// Move resolvers.ts
shelljs_1.default.mv(path.join(projectMainPath, 'generated/graphql/resolvers.ts'), path.join(projectMainPath, 'src/resolvers.ts'));
// Move schema.ts
shelljs_1.default.rm(path.join(projectMainPath, 'generated/graphql/schema.ts'));
// Move helpers
for (const helperFile of shelljs_1.default.ls(path.join(projectMainPath, 'generated/graphql/helpers/*.ts'))) {
const fileName = helperFile.split('/').pop().replace('SpecWrapper.ts', '');
const importPath = shelljs_1.default.grep(`import { ${fileName} }`, helperFile).replace(/.*from\s+["']([^"']+)["'].*/, '$1');
const resolvedPath = importPath.replace('~app/modules', path.join(projectMainPath, 'src/modules'));
const targetDir = `${resolvedPath.replace(fileName, '').replace('\n', '')}test-helpers`;
shelljs_1.default.mkdir('-p', targetDir);
shelljs_1.default.mv(helperFile, `${targetDir}/${fileName}SpecWrapper.ts`);
}
// Update imports in resolvers.ts
shelljs_1.default.sed('-i', /import { (.+?)Resolvers } from "\.\/(.+?)Resolvers";/g, 'import { $1Resolvers } from "~app/modules/$1/graphql/$1Resolvers";', path.join(projectMainPath, 'src/resolvers.ts'));
// Update imports in ./src/ that use path: "~generated/graphql/types"
shelljs_1.default
.find(path.join(projectMainPath, 'src/'))
.filter((file) => file.match(/\.ts$/))
// eslint-disable-next-line unicorn/no-array-for-each
.forEach((file) => {
shelljs_1.default.sed('-i', `import { schema } from "~generated/graphql/schema"`, `import { schema } from "~app/schema"`, file);
shelljs_1.default.sed('-i', `import { resolvers } from "~generated/graphql/resolvers"`, `import { resolvers } from "~app/resolvers"`, file);
shelljs_1.default.sed('-i', `import { Resolvers } from "./types";`, `import { Resolvers } from "~app/graphqlTypes";`, file);
shelljs_1.default.sed('-i', '~generated/graphql/helpers/', './test-helpers/', file);
shelljs_1.default.sed('-i', /~generated\/graphql\/types/g, '~app/graphqlTypes', file);
});
}
class Eject extends core_1.Command {
async run() {
await this.parse(Eject);
(0, assert_git_clean_state_1.assertGitCleanState)();
const chimpMainPath = path.join(__dirname, '../../');
const projectMainPath = (0, findProjectMainPath_1.findProjectMainPath)();
const tasks = (0, ListrHelper_1.setupListr)([
(0, ListrHelper_1.newTask)('Moving code', async () => moveCode(projectMainPath, chimpMainPath)),
(0, ListrHelper_1.newTask)('Installing GraphQL schema tooling', async () => installingTooling(projectMainPath)),
]);
try {
await tasks.run();
}
catch (error) {
console.error(error);
}
}
}
Eject.description = 'Eject from chimp. While chimp is not a runtime dependency and your project will still run after removing it, ejecting offers a cleaner development environment. While we hope you never need to eject, it is reassuring to know you have the option. If you choose to eject or are considering it, please inform us. Remember, you are always just one command away from this choice.';
Eject.examples = ['$ chimp eject'];
Eject.flags = {
help: core_1.Flags.help({ char: 'h' }),
};
exports.default = Eject;
;