Skip to content

Commit

Permalink
feat: support different configuration files for different usecases (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
MKruschke authored Mar 27, 2024
1 parent e67433c commit 4a7a93d
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ npx update-ts-references --help
--createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src"
--cwd Set working directory. Default: /Users/john-doe/projects/my-project
--verbose Show verbose output. Default: false
--usecase Use a specific usecase configuration. Default: update-ts-references.yaml
```

Expand Down Expand Up @@ -99,7 +100,8 @@ Additional to that you can configure also the following options:

Example configuration see [here](./test-scenarios/ts-options-yaml/update-ts-references.yaml)


### using multiple configurations for different usecases
Executing update-ts-references with different configurations via the parameter `--usecase`.

## FAQ
### Why is my pnpm workspace alias not working?
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-ts-references",
"version": "3.2.1",
"version": "3.3.0",
"description": "Updates TypeScript references automatically while using workspaces",
"bin": "src/index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
h = defaultOptions.help,
check = defaultOptions.check,
createPathMappings = defaultOptions.createPathMappings,
usecase = defaultOptions.usecase
} = minimist(process.argv.slice(2));
if (help || h) {
console.log(`
Expand All @@ -27,6 +28,7 @@ if (help || h) {
--createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src"
--cwd Set working directory. Default: ${defaultOptions.cwd}
--verbose Show verbose output. Default: ${defaultOptions.verbose}
--usecase The use case for the script. Default: ${defaultOptions.usecase}
`);
process.exit(0);
}
Expand All @@ -40,7 +42,8 @@ const run = async () => {
configName,
rootConfigName,
createTsConfig,
createPathMappings
createPathMappings,
usecase
});

if (check && changesCount > 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const defaultOptions = {
verbose: false,
help: false,
check: false,
createPathMappings: false
createPathMappings: false,
usecase: 'update-ts-references.yaml'
};

const getAllPackageJsons = async (workspaces, cwd) => {
Expand Down Expand Up @@ -230,6 +231,7 @@ const execute = async ({
cwd, createTsConfig,
verbose,
check,
usecase,
...configurable
}) => {
let changesCount = 0;
Expand Down Expand Up @@ -260,9 +262,9 @@ const execute = async ({
createPathMappings
} = configurable

if (fs.existsSync(path.join(cwd, 'update-ts-references.yaml'))) {
if (fs.existsSync(path.join(cwd, usecase))) {
const yamlConfig = yaml.load(
fs.readFileSync(path.join(cwd, 'update-ts-references.yaml'))
fs.readFileSync(path.join(cwd, usecase))
);
configName = yamlConfig.configName ?? configName
rootConfigName = yamlConfig.rootConfigName ?? rootConfigName
Expand Down
13 changes: 13 additions & 0 deletions test-scenarios/ts-options-usecase-yaml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ts-ref-yaml-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-b",
"shared/*",
"utils/**/"
],
"devDependencies": {
"typescript": "latest"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/ts-options-usecase-yaml/tsconfig.root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": [],
"compilerOptions": {
/* Basic Options */
// "allowJs": true,
"composite": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
configName: 'tsconfig.dev.json'
rootConfigName: 'tsconfig.root.json'
createPathMappings: true
packages:
# all packages in subdirs of packages/ and components/
- 'workspace-a'
# exclude packages that are inside test directories
- '!**/tests/**'
- '!workspace-ignore'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-a",
"version": "1.0.0",
"dependencies": {
"workspace-b": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-options-usecase-yaml/workspace-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-b",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"devDependencies": {
"foo-b": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ignore",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"devDependencies": {
"foo-b": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
4 changes: 2 additions & 2 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const execSh = require('exec-sh').promise;
const fs = require('fs');

const setup = async (rootFolder, configName, rootConfigName, createTsConfig, createPathMappings) => {
const setup = async (rootFolder, configName, rootConfigName, createTsConfig, createPathMappings, usecase) => {
if (!fs.existsSync(rootFolder)) {
throw new Error(`folder is missing -> ${rootFolder}`);
}
Expand All @@ -12,7 +12,7 @@ const setup = async (rootFolder, configName, rootConfigName, createTsConfig, cre
configName ? ` --configName ${configName}` : ''
}${
rootConfigName ? ` --rootConfigName ${rootConfigName}` : ''
}${createTsConfig ? ` --createTsConfig` : ''}${createPathMappings ? ` --createPathMappings` : ''}`,
}${createTsConfig ? ` --createTsConfig` : ''}${createPathMappings ? ` --createPathMappings` : ''}${usecase ? ` --usecase ${usecase}` : ''}`,
{
cwd: rootFolder,
}
Expand Down
57 changes: 57 additions & 0 deletions tests/update-ts-references.yaml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {parse} = require("comment-json")

const rootFolderTsRefYaml = path.join(process.cwd(), 'test-run', 'ts-ref-yaml');
const rootFolderTsOptionsYaml = path.join(process.cwd(), 'test-run', 'ts-options-yaml');
const rootFolderUsecaseYaml = path.join(process.cwd(), 'test-run', 'ts-options-usecase-yaml');

const compilerOptions = {outDir: 'dist', rootDir: 'src'};

Expand Down Expand Up @@ -195,3 +196,59 @@ test('receive options via the config', async () => {
parse(fs.readFileSync(path.join(rootFolderTsOptionsYaml, 'workspace-ignore', 'tsconfig.json')).toString())
).toEqual({compilerOptions});
});

test('receive options for a different usecase', async () => {
await setup(rootFolderUsecaseYaml, undefined, undefined, undefined, undefined, 'update-ts-references.e2e.yaml');
const root = [
'.',
{
compilerOptions: {
composite: true,
},
files: [],
references: [
{
path: 'workspace-a/tsconfig.dev.json',
},
{
path: 'workspace-b/tsconfig.dev.json',
},
],
},
];

const a = [
'./workspace-a',
{
compilerOptions: {
...compilerOptions,
paths: {"workspace-b": ["../workspace-b/src"]}
},
references: [
{
path: '../workspace-b/tsconfig.dev.json',
},
],
},
];

const b = [
'./workspace-b',
{
compilerOptions,
},
];

[root,a,b].forEach((tsconfig) => {
const [configPath, config] = tsconfig;

expect(
parse(fs.readFileSync(path.join(rootFolderUsecaseYaml, configPath, configPath === '.'?'tsconfig.root.json':'tsconfig.dev.json')).toString())
).toEqual(config);
});

// should not touch the ignore config
expect(
parse(fs.readFileSync(path.join(rootFolderUsecaseYaml, 'workspace-ignore', 'tsconfig.json')).toString())
).toEqual({compilerOptions});
});

0 comments on commit 4a7a93d

Please sign in to comment.