Skip to content

Commit

Permalink
feat: add createTsConfig option incl. small fixes (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
MKruschke committed Nov 17, 2023
1 parent 8ae0ed4 commit a588e64
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 30 deletions.
42 changes: 35 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ npx update-ts-references --help
Usage: update-ts-references [options]
Options:
--configName The name of the config files which needs to be updated. Default: tsconfig.json
--check Checks if updates would be necessary (without applying them)
--help Show help
--cwd Set working directory. Default: [current path]
--verbose Show verbose output. Default: false
--configName The name of the config files which needs to be updated. Default: tsconfig.json
--check Checks if updates would be necessary (without applying them)
--help Show help
--createTsConfig Create default TS configs for packages where the main entry in the package.json have a ts|tsx extension (Note: respects the --configName parameter)
--cwd Set working directory. Default: /Users/mirko.kruschke/coding/ecg-public/update-ts-references
--verbose Show verbose output. Default: false
```

or you add it as dev dependency and include it in the `postinstall` script in the package.json
Expand All @@ -37,11 +39,37 @@ or you add it as dev dependency and include it in the `postinstall` script in th
},
```

## FAQ
enable pre-push via husky
```
npx husky add .husky/pre-push "npx update-ts-references --check"
git add .husky/pre-push
```

## using --creatTsConfig
Creates a basic tsconfig file for each package where the main entry in the package.json have a `.ts` or `.tsx` extension. It will respect the `--configName` parameter.

The output for the created file looks like the following

```json
{
"extends": "../tsconfig.base.json", // add's extends in case you have a base config in the root directory
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"references": [ // will be added after running update-ts-references
{
"path": "../some-other-package
}
]
}
```


## FAQ
### Why is my pnpm workspace alias not working?

_update-ts-references_ is currently not supporting [Referencing workspace packages through aliases](https://pnpm.js.org/workspaces#referencing-workspace-packages-through-aliases) yet. See issue #13
_update-ts-references_ is currently not supporting [Referencing workspace packages through aliases](https://pnpm.js.org/workspaces#referencing-workspace-packages-through-aliases) yet. See issue #13

# License

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-ts-references",
"version": "2.6.1",
"version": "2.7.0",
"description": "Updates TypeScript references automatically while using workspaces",
"bin": "src/index.js",
"scripts": {
Expand All @@ -23,7 +23,7 @@
"comment-json": "^4.2.3",
"glob": "^7.1.6",
"js-yaml": "^4.0.0",
"minimatch": "^3.0.4",
"minimatch": "^3.0.5",
"minimist": "^1.2.5",
"mkdirp": "^1.0.4"
},
Expand All @@ -39,7 +39,7 @@
"license": "MIT",
"keywords": [
"typescript",
"references",
"project references",
"tool",
"util",
"yarn",
Expand Down
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ const { execute, defaultOptions } = require('./update-ts-references');

const {
configName = defaultOptions.configName,
createTsConfig = defaultOptions.createTsConfig,
cwd = defaultOptions.cwd,
verbose = defaultOptions.verbose,
help = defaultOptions.help,
h = defaultOptions.help,
check = defaultOptions.check,
} = minimist(process.argv.slice(2));

console.log('->',createTsConfig)
if (help || h) {
console.log(`
Usage: update-ts-references [options]
Options:
--configName The name of the config files which needs to be updated. Default: ${defaultOptions.configName}
--check Checks if updates would be necessary (without applying them)
--help Show help
--cwd Set working directory. Default: ${defaultOptions.cwd}
--verbose Show verbose output. Default: ${defaultOptions.verbose}
--configName The name of the config files which needs to be updated. Default: ${defaultOptions.configName}
--check Checks if updates would be necessary (without applying them)
--help Show help
--createTsConfig Create default TS configs for packages where the main entry in the package.json have a ts|tsx extension (Note: respects the --configName parameter)
--cwd Set working directory. Default: ${defaultOptions.cwd}
--verbose Show verbose output. Default: ${defaultOptions.verbose}
`);
process.exit(0);
}
Expand All @@ -33,6 +35,7 @@ const run = async () => {
verbose,
check,
configName,
createTsConfig
});

if (check && changesCount > 0) {
Expand Down
48 changes: 36 additions & 12 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ const TSCONFIG_JSON = 'tsconfig.json'

const defaultOptions = {
configName: 'tsconfig.json',
createTsConfig: false,
cwd: process.cwd(),
verbose: false,
help: false,
check: false,
};

const getAllPackageJsons = async (workspaces) => {
const getAllPackageJsons = async (workspaces,cwd) => {
const ignoreGlobs = [];
const workspaceGlobs = [];

Expand All @@ -37,7 +38,7 @@ const getAllPackageJsons = async (workspaces) => {
workspaceGlobs.map(
(workspace) =>
new Promise((resolve, reject) => {
glob(`${workspace}/${PACKAGE_JSON}`, (error, files) => {
glob(`${workspace}/${PACKAGE_JSON}`, {cwd},(error, files) => {
if (error) {
reject(error);
}
Expand Down Expand Up @@ -65,21 +66,42 @@ const getAllPackageJsons = async (workspaces) => {
);
};

const detectTSConfig = (directory, configName) => {
const detectTSConfig = (directory, configName, createConfig,cwd) => {
let detectedConfig = fs.existsSync(path.join(directory, configName)) ? configName : null
if (configName !== TSCONFIG_JSON && detectedConfig === null) {
detectedConfig = fs.existsSync(path.join(directory, TSCONFIG_JSON)) ? TSCONFIG_JSON : null
}
if(detectedConfig === null && createConfig) {
let maybeExtends = {}
if(fs.existsSync(path.join(cwd, 'tsconfig.base.json'))) {
maybeExtends = {
extends: `${path.join(path.relative(directory,cwd),"tsconfig.base.json").split(path.sep).join(path.posix.sep)}`,
}
}
const tsconfigFilePath = path.join(directory, configName);
fs.writeFileSync(tsconfigFilePath, stringify(Object.assign(maybeExtends,{
compilerOptions: {
outDir: "dist",
rootDir: "src"
},
references: [],
}), null, 2) + '\n');

return configName
}
return detectedConfig
}


const getPackageNamesAndPackageDir = (packageFilePaths) =>
const getPackageNamesAndPackageDir = (packageFilePaths, cwd) =>
packageFilePaths.reduce((map, packageFilePath) => {
const fullPackageFilePath = path.join(process.cwd(), packageFilePath);
const fullPackageFilePath = path.join(cwd, packageFilePath);
const packageJson = require(fullPackageFilePath);
const { name } = packageJson;
map.set(name, { packageDir: path.dirname(fullPackageFilePath) });
map.set(name, {
packageDir: path.dirname(fullPackageFilePath),
hasTsEntry: /\.(ts|tsx)$/.test((packageJson.main ? packageJson.main :''))
});
return map;
}, new Map());

Expand Down Expand Up @@ -112,6 +134,7 @@ const getReferencesFromDependencies = (
}

return Object.keys(mergedDependencies)
.filter(name => !name.includes('test-utils'))
.reduce((referenceArray, dependency) => {
if (packagesMap.has(dependency)) {
const { packageDir: dependencyDir } = packagesMap.get(dependency);
Expand Down Expand Up @@ -180,7 +203,7 @@ const updateTsConfig = (
};

const execute = async ({
cwd,
cwd, createTsConfig,
verbose,
check,
configName,
Expand Down Expand Up @@ -213,11 +236,11 @@ const execute = async ({
workspaces = workspaces.packages;
}

const packageFilePaths = await getAllPackageJsons(workspaces);
const packageFilePaths = await getAllPackageJsons(workspaces, cwd);
if (verbose) {
console.log('packageFilePaths', packageFilePaths);
}
const packagesMap = getPackageNamesAndPackageDir(packageFilePaths);
const packagesMap = getPackageNamesAndPackageDir(packageFilePaths, cwd);

if (verbose) {
console.log('packagesMap', packagesMap);
Expand All @@ -226,11 +249,11 @@ const execute = async ({
const rootReferences = [];

packagesMap.forEach((packageEntry, packageName) => {
const detectedConfig = detectTSConfig(packageEntry.packageDir, configName)
const detectedConfig = detectTSConfig(packageEntry.packageDir, configName, packageEntry.hasTsEntry && createTsConfig,cwd)

if (detectedConfig) {
rootReferences.push({
path: path.join(path.relative(process.cwd(), packageEntry.packageDir), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''),
path: path.join(path.relative(cwd, packageEntry.packageDir), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''),
});
const references = getReferencesFromDependencies(
configName,
Expand Down Expand Up @@ -260,7 +283,8 @@ const execute = async ({
changesCount += updateTsConfig(
configName,
rootReferences,
check
check, {packageDir:cwd}

);

if (verbose) {
Expand Down
14 changes: 14 additions & 0 deletions test-scenarios/yarn-ws-create/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "yarn-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-a",
"workspace-b",
"shared/*",
"utils/**"
],
"devDependencies": {
"typescript": "latest"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/yarn-ws-create/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": [],
"compilerOptions": {
/* Basic Options */
// "allowJs": true,
"composite": true
}
}
9 changes: 9 additions & 0 deletions test-scenarios/yarn-ws-create/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": ["./tsconfig.base.json"],
"files": [],
"compilerOptions": {
/* Basic Options */
// "allowJs": true,
"composite": true
}
}
11 changes: 11 additions & 0 deletions test-scenarios/yarn-ws-create/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "workspace-a",
"version": "1.0.0",
"main": "src/index.ts",
"dependencies": {
"workspace-b": "1.0.0"
},
"devDependencies": {
"foo-a": "1.0.0"
}
}
Empty file.
7 changes: 7 additions & 0 deletions test-scenarios/yarn-ws-create/workspace-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-b",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/yarn-ws-create/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/yarn-ws-create/workspace-c/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "workspace-b",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"cross-env": "5.0.5"
}
}

0 comments on commit a588e64

Please sign in to comment.