Skip to content

Instantly share code, notes, and snippets.

@mrgrain
Last active June 5, 2023 20:28
Show Gist options
  • Save mrgrain/db41783b66d80d85afb2a58b14949d18 to your computer and use it in GitHub Desktop.
Save mrgrain/db41783b66d80d85afb2a58b14949d18 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const { spawnSync } = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
const spawny = (command) => {
const [cmd, ...rest] = command.split(" ");
console.log(command);
spawnSync(cmd, rest, { stdio: 'inherit', cwd: process.cwd() });
}
const npxProjen = () => spawny('npx projen default')
const rcJsName = process.argv.slice(2).at(0) ?? ".projenrc.js";
const rcTsName = '.projenrc.ts';
const rcFilePath = path.join(process.cwd(), rcJsName);
/**
* Add projenrcTs: true to your project.
*/
const rcFileBuffer = fs.readFileSync(rcFilePath, { encoding: 'utf8' }).split('\n');
const nameLine = rcFileBuffer.findIndex((l) => l.match(/name\:.*,/))
rcFileBuffer.splice(nameLine + 1, 0, ' projenrcTs: true,')
fs.writeFileSync(rcFilePath, rcFileBuffer.join('\n'));
/**
* Run npx projen.
*/
npxProjen();
/**
* Rename .projenrc.js to .projenrc.ts.
*/
const newRcFilePath = path.join(process.cwd(), rcTsName);
fs.renameSync(rcFilePath, newRcFilePath);
/**
* Update requires to imports.
*/
spawny(`npx --yes cjs-to-es6@2.0.1 ${rcTsName}`)
/**
* Run npx projen.
*/
npxProjen();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment