|
| 1 | +import { |
| 2 | + rename, |
| 3 | + access, |
| 4 | + writeFile, |
| 5 | + rm, |
| 6 | + realpath, |
| 7 | + constants as fsConstants |
| 8 | +} from 'node:fs/promises'; |
| 9 | +import { dirname, join } from 'node:path'; |
| 10 | +import { promisify } from 'node:util'; |
| 11 | +import { execFile } from 'node:child_process'; |
| 12 | + |
| 13 | +const pExecFile = promisify(execFile); |
| 14 | + |
| 15 | +async function restore (cwd) { |
| 16 | + const pkgPath = join(cwd, 'package.json'); |
| 17 | + const pkgBakPath = join(cwd, 'package.json.bak'); |
| 18 | + try { |
| 19 | + await access(pkgBakPath, fsConstants.R_OK | fsConstants.W_OK); |
| 20 | + console.log('package.json.bak exists, restoring original'); |
| 21 | + await rm(pkgPath); |
| 22 | + await rename(pkgBakPath, pkgPath, fsConstants.COPYFILE_FICLONE); |
| 23 | + } catch (e) { |
| 24 | + if (e.code !== 'ENOENT') { |
| 25 | + throw e; |
| 26 | + } |
| 27 | + } |
| 28 | + return { pkgPath, pkgBakPath }; |
| 29 | +} |
| 30 | + |
| 31 | +export async function setup (cwd, opts = {}) { |
| 32 | + // Ensure we have the right node version |
| 33 | + if (process.version !== `v${opts.node}`) { |
| 34 | + // TODO: install node with nvm? |
| 35 | + throw Object.assign(new Error(`Incorrect node.js version`), { |
| 36 | + wanted: opts.node, |
| 37 | + found: process.version.replace(/^v/, '') |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + // Setup the repo if asked |
| 42 | + if (!opts.repo) { |
| 43 | + console.log('Using local repo.'); |
| 44 | + } else { |
| 45 | + // TODO: clone repo into tmp dir |
| 46 | + throw new Error('cloning repo not yet implemented'); |
| 47 | + } |
| 48 | + |
| 49 | + // Restore backup if necessary |
| 50 | + const { pkgPath, pkgBakPath } = await restore(cwd); |
| 51 | + |
| 52 | + // Read in package.json contets |
| 53 | + const pkg = await import(pkgPath, { |
| 54 | + with: { |
| 55 | + type: 'json' |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + // Apply overrides |
| 60 | + if (opts.overrides) { |
| 61 | + await rename(pkgPath, pkgBakPath); |
| 62 | + pkg.overrides = { |
| 63 | + ...(pkg.overrides ?? {}), |
| 64 | + ...opts.overrides |
| 65 | + }; |
| 66 | + await writeFile(pkgPath, JSON.stringify(pkg, null, 2)); |
| 67 | + } |
| 68 | + |
| 69 | + // node --run setup || npm run setup |
| 70 | + try { |
| 71 | + const nodePath = await realpath(process.execPath); |
| 72 | + await pExecFile(nodePath, ['--run', 'setup'], { cwd }); |
| 73 | + } catch (e) { |
| 74 | + const npmPath = await realpath(join(dirname(process.execPath), 'npm')); |
| 75 | + await pExecFile(npmPath, ['run', 'setup'], { cwd }); |
| 76 | + } |
| 77 | + |
| 78 | + return { cwd }; |
| 79 | +} |
| 80 | + |
| 81 | +export async function cleanup (cwd) { |
| 82 | + await restore(cwd); |
| 83 | + await rm(join(cwd, 'package-lock.json'), { |
| 84 | + force: true |
| 85 | + }); |
| 86 | + await rm(join(cwd, 'node_modules'), { |
| 87 | + force: true, |
| 88 | + recursive: true |
| 89 | + }); |
| 90 | +} |
0 commit comments