|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import { cp } from 'fs/promises';
- import { resolve } from 'path';
- import { Argv } from 'yargs';
-
- const copyFiles = async () => {
- const srcPath = resolve(__dirname, '..', '..', '..', 'src', 'next');
- const destPath = resolve(__dirname, '..', '..', '..', '.amanuensis', 'next');
- await cp(srcPath, destPath, { recursive: true });
- };
-
- interface PackageManager {
- name: string;
- testCmd: [string, string[]];
- installCmd: [string, string[]];
- }
-
- const packageManagers: PackageManager[] = [
- {
- name: 'pnpm',
- testCmd: ['pnpm', ['--version']],
- installCmd: ['pnpm', ['install', '--ignore-workspace']],
- },
- {
- name: 'yarn',
- testCmd: ['yarn', ['--version']],
- installCmd: ['yarn', ['install']],
- },
- {
- name: 'npm',
- testCmd: ['npm', ['--version']],
- installCmd: ['npm', ['install']],
- },
- ];
-
- const installDependencies = async () => {
- const { execa } = await import('execa');
-
- const selectedPackageManagerIndex = await packageManagers.reduce(
- async (prevPmPromise, pkgm, i) => {
- const prevPm = await prevPmPromise;
- const { testCmd } = pkgm;
- const testPm = await execa(...testCmd);
- if (testPm.exitCode === 0 && prevPm === -1) {
- return i;
- }
- return prevPm;
- },
- Promise.resolve(-1),
- );
-
- if (selectedPackageManagerIndex < 0) {
- process.stderr.write('No package manager found\n');
- process.exit(-1);
- }
-
- const { [selectedPackageManagerIndex]: selectedPackageManager } = packageManagers;
- const cwd = resolve(__dirname, '..', '..', '..', '.amanuensis', 'next');
- process.stdout.write(`In path: ${cwd}\n`);
- process.stdout.write(`Installing dependencies with ${selectedPackageManager.name}\n`);
- await execa(
- selectedPackageManager.installCmd[0],
- selectedPackageManager.installCmd[1],
- { cwd },
- );
- };
-
- export const description = 'Initialize a new Amanuensis project' as const;
-
- export const builder = (yargsBuilder: Argv) => yargsBuilder;
-
- const init = async () => {
- await copyFiles();
- await installDependencies();
- return 0;
- };
-
- export default init;
|