typescript-monorepo-toolkit
Advanced tools
Comparing version 0.0.7 to 0.1.0
"use strict"; | ||
// isolated due to parser changes ts 3.7 | ||
// https://github.com/microsoft/TypeScript/pull/32695 | ||
// eslint-disable-next-line prettier/prettier | ||
// export function assertNonNull<T>(v: T): asserts v is NonNullable<T> { | ||
// if (v === undefined || v === null) { | ||
// throw new Error("assertNonNull"); | ||
// } | ||
// } | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @template T | ||
* @param {T} v | ||
* @return {asserts v is NonNullable<T>} | ||
*/ | ||
exports.assertNonNull = void 0; | ||
function assertNonNull(v) { | ||
@@ -17,0 +5,0 @@ if (v === undefined || v === null) { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.injectRefs = void 0; | ||
const helpers_1 = require("./helpers"); | ||
const pMap = require("p-map"); | ||
const debug = require("debug"); | ||
const path = require("path"); | ||
const path_1 = __importDefault(require("path")); | ||
const fs_1 = require("fs"); | ||
@@ -14,8 +18,8 @@ const debugFunc = debug("typescript-monorepo-toolkit"); | ||
debugFunc("found %d none-ts packages(?), ignoring them", noneTsPackages.length); | ||
debugFunc("none ts: %j", noneTsPackages.map(p => p[0])); | ||
debugFunc("none ts: %j", noneTsPackages.map((p) => p[0])); | ||
const map = new Map(tsPackages); | ||
await pMap(tsPackages, p => { | ||
await pMap(tsPackages, (p) => { | ||
helpers_1.applyOnPackage(projectRoot, p[0], map, options.tsconfigPathInsidePackages); | ||
}, { | ||
concurrency: 4 | ||
concurrency: 4, | ||
}); | ||
@@ -29,7 +33,7 @@ // packages no-one is depending on | ||
references: topLevelPackages.map(([, info]) => ({ | ||
path: path.relative(projectRoot, path.resolve(projectRoot, info.location)) | ||
path: path_1.default.relative(projectRoot, path_1.default.resolve(projectRoot, info.location)), | ||
})), | ||
include: [] | ||
include: [], | ||
}; | ||
await fs_1.promises.writeFile(path.resolve(projectRoot, "build-all-tsconfig.json"), JSON.stringify(buildAllTSConfig, null, 2)); | ||
await fs_1.promises.writeFile(path_1.default.resolve(projectRoot, "build-all-tsconfig.json"), JSON.stringify(buildAllTSConfig, null, 2)); | ||
console.log("build-all-tsconfig.json created/updated"); | ||
@@ -36,0 +40,0 @@ } |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.readWorkspaceInfoObject = exports.applyTransformationOnAllPackages = exports.setCompilerOptionsStringProp = exports.setRootStringProp = exports.ensureCompositeProject = exports.findEnsureProjectReferences = exports.findEnsureCompilerOptions = exports.setProjectReferences = exports.applyTransformationOnTSConfig = exports.applyOnPackage = exports.partitionNoneTs = exports.execFile = exports.wrapAsyncCommand = void 0; | ||
const recast_1 = require("recast"); | ||
const childProcess = require("child_process"); | ||
const path = require("path"); | ||
const child_process_1 = __importDefault(require("child_process")); | ||
const path_1 = __importDefault(require("path")); | ||
const fs_1 = require("fs"); | ||
const pMap = require("p-map"); | ||
const partition = require("lodash.partition"); | ||
const debug = require("debug"); | ||
const p_map_1 = __importDefault(require("p-map")); | ||
const lodash_partition_1 = __importDefault(require("lodash.partition")); | ||
const debug_1 = __importDefault(require("debug")); | ||
const assertNonNull_1 = require("./assertNonNull"); | ||
const recast_2 = require("recast"); | ||
const debugFunc = debug("typescript-monorepo-toolkit"); | ||
const workspace_info_1 = require("workspace-info"); | ||
const debugFunc = debug_1.default("typescript-monorepo-toolkit"); | ||
function wrapAsyncCommand(command) { | ||
command.catch(error => { | ||
command.catch((error) => { | ||
console.error("command exited with an error", error); | ||
@@ -22,4 +27,4 @@ process.exit(1); | ||
return new Promise((res, rej) => { | ||
childProcess.execFile(file, args, { | ||
cwd | ||
child_process_1.default.execFile(file, args, { | ||
cwd, | ||
}, (error, stdOut) => { | ||
@@ -37,3 +42,3 @@ if (error) { | ||
async function partitionNoneTs(root, input, tsconfigPath) { | ||
return pMap(input, async ([packageName, packageInfo]) => { | ||
return p_map_1.default(input, async ([packageName, packageInfo]) => { | ||
const is = await isTsPackage(root, packageName, packageInfo, tsconfigPath); | ||
@@ -43,11 +48,11 @@ return { | ||
packageName, | ||
packageInfo | ||
packageInfo, | ||
}; | ||
}, { | ||
concurrency: 4 | ||
}).then(all => { | ||
const [ts, noneTs] = partition(all, e => e.is); | ||
concurrency: 4, | ||
}).then((all) => { | ||
const [ts, noneTs] = lodash_partition_1.default(all, (e) => e.is); | ||
return [ | ||
ts.map(e => [e.packageName, e.packageInfo]), | ||
noneTs.map(e => [e.packageName, e.packageInfo]) | ||
ts.map((e) => [e.packageName, e.packageInfo]), | ||
noneTs.map((e) => [e.packageName, e.packageInfo]), | ||
]; | ||
@@ -68,3 +73,3 @@ // tuple map ain't smart enough | ||
return await fs_1.promises | ||
.access(path.resolve(root, packageInfo.location, tsconfigPath), fs_1.constants.F_OK) | ||
.access(path_1.default.resolve(root, packageInfo.location, tsconfigPath), fs_1.constants.F_OK) | ||
.then(() => true, () => false); | ||
@@ -75,15 +80,13 @@ } | ||
assertNonNull_1.assertNonNull(packageInfo); | ||
const absolutePath = path.resolve(root, packageInfo.location); | ||
const tsconfigContent = await fs_1.promises.readFile(path.resolve(root, packageInfo.location, tsconfigPath), "utf-8"); | ||
const absolutePath = path_1.default.resolve(root, packageInfo.location); | ||
const tsconfigContent = await fs_1.promises.readFile(path_1.default.resolve(root, packageInfo.location, tsconfigPath), "utf-8"); | ||
const asJs = `[${tsconfigContent}]`; | ||
const ast = recast_2.parse(asJs, {}); | ||
const deps = packageInfo.workspaceDependencies | ||
.map(depPackageName => packagesMap.get(depPackageName)) | ||
.map((depPackageName) => packagesMap.get(depPackageName)) | ||
.filter((v) => v !== undefined); | ||
const paths = deps.map(d => path.relative(absolutePath, path.resolve(root, d.location))); | ||
const paths = deps.map((d) => path_1.default.relative(absolutePath, path_1.default.resolve(root, d.location))); | ||
ensureCompositeProject(ast); | ||
setProjectReferences(ast, paths); | ||
await fs_1.promises.writeFile(path.resolve(root, packageInfo.location, tsconfigPath), recast_2.print(ast) | ||
.code.substring(1) | ||
.slice(0, -1)); | ||
await fs_1.promises.writeFile(path_1.default.resolve(root, packageInfo.location, tsconfigPath), recast_2.print(ast).code.substring(1).slice(0, -1)); | ||
} | ||
@@ -94,11 +97,10 @@ exports.applyOnPackage = applyOnPackage; | ||
assertNonNull_1.assertNonNull(packageInfo); | ||
const tsconfigContent = await fs_1.promises.readFile(path.resolve(root, packageInfo.location, tsconfigPath), "utf-8"); | ||
const tsconfigContent = await fs_1.promises.readFile(path_1.default.resolve(root, packageInfo.location, tsconfigPath), "utf-8"); | ||
const asJs = `[${tsconfigContent}]`; | ||
const ast = recast_2.parse(asJs, {}); | ||
await transformation(ast, root, packageName, packageInfo); | ||
await fs_1.promises.writeFile(path.resolve(root, packageInfo.location, tsconfigPath), recast_2.print(ast) | ||
.code.substring(1) | ||
.slice(0, -1)); | ||
await fs_1.promises.writeFile(path_1.default.resolve(root, packageInfo.location, tsconfigPath), recast_2.print(ast).code.substring(1).slice(0, -1)); | ||
} | ||
exports.applyTransformationOnTSConfig = applyTransformationOnTSConfig; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function setProjectReferences(ast, pathsToAdd) { | ||
@@ -109,3 +111,3 @@ const refsNode = findEnsureProjectReferences(ast); | ||
refsNode.value.elements.push(recast_1.types.builders.objectExpression([ | ||
recast_1.types.builders.property("init", recast_1.types.builders.literal("path"), recast_1.types.builders.literal(path)) | ||
recast_1.types.builders.property("init", recast_1.types.builders.literal("path"), recast_1.types.builders.literal(path)), | ||
])); | ||
@@ -115,2 +117,3 @@ } | ||
exports.setProjectReferences = setProjectReferences; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function findEnsureCompilerOptions(ast) { | ||
@@ -126,2 +129,3 @@ let compilerOptions = ast.program.body[0].expression.elements[0].properties.find((p) => p.key.value === "compilerOptions"); | ||
exports.findEnsureCompilerOptions = findEnsureCompilerOptions; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function findEnsureProjectReferences(ast) { | ||
@@ -137,2 +141,3 @@ let references = ast.program.body[0].expression.elements[0].properties.find((p) => p.key.value === "references"); | ||
exports.findEnsureProjectReferences = findEnsureProjectReferences; | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function ensureCompositeProject(ast) { | ||
@@ -148,3 +153,26 @@ const compilerOptions = findEnsureCompilerOptions(ast); | ||
exports.ensureCompositeProject = ensureCompositeProject; | ||
function setCompilerOptionsStringProp(ast, propName, propValue) { | ||
function setRootStringProp( | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
ast, propName, propValue) { | ||
const root = ast.program.body[0].expression.elements[0]; | ||
let propAst = root.properties.find((p) => p.key.value === propName); | ||
if (!propAst && propValue) { | ||
propAst = recast_1.types.builders.objectProperty(recast_1.types.builders.literal(propName), recast_1.types.builders.stringLiteral(propValue)); | ||
root.properties.push(propAst); | ||
} | ||
if (!propValue) { | ||
if (propAst) { | ||
// delete prop | ||
const index = root.properties.indexOf(propAst); | ||
root.properties.splice(index, 1); | ||
} | ||
} | ||
else { | ||
propAst.value = recast_1.types.builders.stringLiteral(propValue); | ||
} | ||
} | ||
exports.setRootStringProp = setRootStringProp; | ||
function setCompilerOptionsStringProp( | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
ast, propName, propValue) { | ||
const compilerOptions = findEnsureCompilerOptions(ast); | ||
@@ -173,8 +201,8 @@ let propAst = compilerOptions.value.properties.find((p) => p.key.value === propName); | ||
debugFunc("found %d none-ts packages(?), ignoring them", noneTsPackages.length); | ||
debugFunc("none ts: %j", noneTsPackages.map(p => p[0])); | ||
debugFunc("none ts: %j", noneTsPackages.map((p) => p[0])); | ||
const map = new Map(tsPackages); | ||
await pMap(tsPackages, p => { | ||
await p_map_1.default(tsPackages, (p) => { | ||
applyTransformationOnTSConfig(projectRoot, p[0], map, tsconfigPathInsidePackages, transformation); | ||
}, { | ||
concurrency: 4 | ||
concurrency: 4, | ||
}); | ||
@@ -184,10 +212,6 @@ } | ||
async function readWorkspaceInfoObject(projectRoot) { | ||
const r = await execFile("yarn", ["-s", "workspaces", "info", "--json"], projectRoot); | ||
try { | ||
return JSON.parse(JSON.parse(r).data); | ||
} | ||
catch (e) { | ||
return JSON.parse(r); | ||
} | ||
return await workspace_info_1.getWorkspaceInfo({ | ||
cwd: projectRoot, | ||
}); | ||
} | ||
exports.readWorkspaceInfoObject = readWorkspaceInfoObject; |
"use strict"; | ||
// How to preserve comments? use jscodeshift? | ||
// https://github.com/json5/json5/issues/177 | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const commander = require("commander"); | ||
const debug = require("debug"); | ||
const commander_1 = __importDefault(require("commander")); | ||
const debug_1 = __importDefault(require("debug")); | ||
const path_1 = __importDefault(require("path")); | ||
const helpers_1 = require("./helpers"); | ||
const colors = require("colors"); | ||
const commands_1 = require("./commands"); | ||
const debugFunc = debug("typescript-monorepo-toolkit"); | ||
const program = new commander.Command(); | ||
const debugFunc = debug_1.default("typescript-monorepo-toolkit"); | ||
const program = new commander_1.default.Command(); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
program.version(require("../package.json").version); | ||
@@ -21,7 +26,7 @@ program.option("-p, --tsconfig-path <tsconfigpath>", "Use alterative config path inside the package. eg: test/tsconfig.json", "tsconfig.json"); | ||
yarnWorkspaceRoot, | ||
cmdObj | ||
cmdObj, | ||
}); | ||
helpers_1.wrapAsyncCommand(commands_1.injectRefs(yarnWorkspaceRoot, { | ||
generateBuildAll: cmdObj.generateBuildAll, | ||
tsconfigPathInsidePackages: program.tsconfigPath | ||
tsconfigPathInsidePackages: program.tsconfigPath, | ||
})); | ||
@@ -45,2 +50,12 @@ }); | ||
}); | ||
program | ||
.command("set-extend <yarn-project-root> [extendedTsconfigPath]") | ||
.description("Set the compilerOptions.rootDir in all of the packages. omit new value to delete") | ||
.action((yarnWorkspaceRoot, extendedTsconfigPath) => { | ||
extendedTsconfigPath = path_1.default.resolve(extendedTsconfigPath); | ||
helpers_1.wrapAsyncCommand(helpers_1.applyTransformationOnAllPackages(yarnWorkspaceRoot, program.tsconfigPath, async (ast, root, __, info) => { | ||
const localExtendPath = path_1.default.relative(path_1.default.resolve(root, info.location), extendedTsconfigPath); | ||
helpers_1.setRootStringProp(ast, "extends", localExtendPath); | ||
})); | ||
}); | ||
if (!process.argv.slice(2).length) { | ||
@@ -47,0 +62,0 @@ program.outputHelp(makeRed); |
"use strict"; | ||
/* eslint-env jest */ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const recast_1 = require("recast"); | ||
const fs_1 = require("fs"); | ||
const path = require("path"); | ||
const path = __importStar(require("path")); | ||
const helpers_1 = require("./helpers"); | ||
const jest_diff_1 = require("jest-diff"); | ||
const json5 = require("json5"); | ||
const jest_diff_1 = __importDefault(require("jest-diff")); | ||
const json5 = __importStar(require("json5")); | ||
const assert_1 = __importDefault(require("assert")); | ||
describe("modify tsconfig", () => { | ||
@@ -70,1 +93,26 @@ test("ensure composite", async () => { | ||
}); | ||
describe("setRootStringProp", () => { | ||
it("adds missing", () => { | ||
const asString = JSON.stringify({ | ||
compilerOptions: {}, | ||
include: [], | ||
}); | ||
const itsJSwinkwink = `[${asString}]`; | ||
const ast = recast_1.parse(itsJSwinkwink); | ||
helpers_1.setRootStringProp(ast, "extends", "ciao"); | ||
const objectAfter = JSON.parse(recast_1.print(ast).code)[0]; | ||
assert_1.default(objectAfter.extends === "ciao"); | ||
}); | ||
it("replace", () => { | ||
const asString = JSON.stringify({ | ||
compilerOptions: {}, | ||
include: [], | ||
extends: "hello", | ||
}); | ||
const itsJSwinkwink = `[${asString}]`; | ||
const ast = recast_1.parse(itsJSwinkwink); | ||
helpers_1.setRootStringProp(ast, "extends", "another_one"); | ||
const objectAfter = JSON.parse(recast_1.print(ast).code)[0]; | ||
assert_1.default(objectAfter.extends === "another_one"); | ||
}); | ||
}); |
{ | ||
"name": "typescript-monorepo-toolkit", | ||
"version": "0.0.7", | ||
"version": "0.1.0", | ||
"author": "Bnaya Peretz <me@bnaya.net>", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=10 <15" | ||
}, | ||
"repository": { | ||
@@ -21,25 +24,25 @@ "type": "git", | ||
"devDependencies": { | ||
"@babel/core": "^7.8.7", | ||
"@babel/preset-env": "^7.8.7", | ||
"@babel/preset-typescript": "^7.8.3", | ||
"@babel/core": "^7.11.4", | ||
"@babel/preset-env": "^7.11.0", | ||
"@babel/preset-typescript": "^7.10.4", | ||
"@types/commander": "^2.12.2", | ||
"@types/debug": "^4.1.5", | ||
"@types/jest": "^25.1.4", | ||
"@types/jest": "^26.0.10", | ||
"@types/json5": "^0.0.30", | ||
"@types/lodash.partition": "^4.6.6", | ||
"@types/node": "^12", | ||
"@types/node": "^14.6.1", | ||
"@types/p-map": "^2.0.0", | ||
"@typescript-eslint/eslint-plugin": "^2.23.0", | ||
"@typescript-eslint/parser": "^2.23.0", | ||
"concurrently": "^5.1.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"husky": "^4.2.3", | ||
"jest": "^25.1.0", | ||
"json5": "^2.1.1", | ||
"prettier": "^1.19.1", | ||
"ts-node": "^8.6.2", | ||
"typescript": "^3.8.3", | ||
"yarn-deduplicate": "^2.0.0" | ||
"@typescript-eslint/eslint-plugin": "^3.10.1", | ||
"@typescript-eslint/parser": "^3.10.1", | ||
"concurrently": "^5.3.0", | ||
"eslint": "^7.7.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"husky": "^4.2.5", | ||
"jest": "^26.4.2", | ||
"json5": "^2.1.3", | ||
"prettier": "^2.1.1", | ||
"ts-node": "^9.0.0", | ||
"typescript": "^4.0.2", | ||
"yarn-deduplicate": "^2.1.1" | ||
}, | ||
@@ -60,7 +63,8 @@ "husky": { | ||
"colors": "^1.4.0", | ||
"commander": "^4.1.1", | ||
"commander": "^6.1.0", | ||
"debug": "^4.1.1", | ||
"lodash.partition": "^4.6.0", | ||
"p-map": "^4.0.0", | ||
"recast": "^0.18.7" | ||
"recast": "^0.20.2", | ||
"workspace-info": "^0.0.5" | ||
}, | ||
@@ -67,0 +71,0 @@ "files": [ |
24893
437
7
8
+ Addedworkspace-info@^0.0.5
+ Added@npmcli/map-workspaces@0.0.0-pre.1(transitive)
+ Added@npmcli/name-from-folder@1.0.1(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedast-types@0.14.2(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedcliui@6.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcommander@6.2.1(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedjson-parse-even-better-errors@2.3.1(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addednpm-normalize-package-bin@1.0.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedread-package-json-fast@1.2.2(transitive)
+ Addedrecast@0.20.5(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@2.0.0(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedtslib@2.8.1(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedworkspace-info@0.0.5(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedy18n@4.0.3(transitive)
+ Addedyargs@15.4.1(transitive)
+ Addedyargs-parser@18.1.3(transitive)
- Removedast-types@0.13.3(transitive)
- Removedcommander@4.1.1(transitive)
- Removedprivate@0.1.8(transitive)
- Removedrecast@0.18.10(transitive)
Updatedcommander@^6.1.0
Updatedrecast@^0.20.2