Skip to content

Commit 45f3b42

Browse files
committed
update scripts to work better with workspaces
1 parent ddbbb94 commit 45f3b42

File tree

3 files changed

+58
-107
lines changed

3 files changed

+58
-107
lines changed

scripts/demos-gen-overrides.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

scripts/demos-use-latest.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const linkDemo = async (demoName: string) => {
4242
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
4343

4444
// Track changed files
45-
let changes = 0;
45+
let changes = false;
46+
4647
const updateDeps = (deps: { [key: string]: string }) => {
4748
for (const dep in deps) {
4849
const matchingPackage = workspacePackages.find((p) => p.manifest.name === dep);
@@ -55,12 +56,11 @@ const linkDemo = async (demoName: string) => {
5556
}
5657
latestVersion = '^' + latestVersion;
5758
if (deps[dep] === latestVersion) {
58-
console.log(`- ${dep}: '${deps[dep]}' => '${latestVersion}' (no changes)`);
5959
continue;
6060
}
6161
console.log(`- ${dep}: '${deps[dep]}' => '${latestVersion}'`);
6262
deps[dep] = latestVersion;
63-
changes++;
63+
changes = true;
6464
}
6565
};
6666

@@ -78,6 +78,37 @@ const linkDemo = async (demoName: string) => {
7878

7979
if (changes) {
8080
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`, 'utf8');
81+
} else {
82+
console.log('- No changes');
83+
}
84+
85+
const tsConfigPath = path.join(demoSrc, 'tsconfig.json');
86+
if (fs.existsSync(tsConfigPath)) {
87+
changes = false;
88+
89+
console.log('Checking tsconfig.json');
90+
const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf8'));
91+
92+
if ('references' in tsConfig) {
93+
for (let i = 0; i < tsConfig.references.length; i++) {
94+
const ref = tsConfig.references[i];
95+
if (ref.path === 'tsconfig.workspace.json') {
96+
console.log('- Unlinking tsconfig.workspace.json');
97+
tsConfig.references.splice(i);
98+
changes = true;
99+
}
100+
}
101+
}
102+
103+
if (tsConfig.references.length === 0) {
104+
delete tsConfig.references;
105+
}
106+
107+
if (changes) {
108+
fs.writeFileSync(tsConfigPath, `${JSON.stringify(tsConfig, null, 2)}\n`, 'utf8');
109+
} else {
110+
console.log('- No changes');
111+
}
81112
}
82113
};
83114

@@ -130,7 +161,7 @@ const main = () => {
130161
console.log(`- ${demoName}`);
131162
try {
132163
// Patchy solution with rm -rf'ing node_modules because pnpm tries to reuse existing linked package
133-
execSync('rm -rf node_modules && pnpm --ignore-workspace install', { cwd: demoSrc, stdio: 'inherit' });
164+
execSync('rm -rf node_modules && pnpm install', { cwd: demoSrc, stdio: 'inherit' });
134165
} catch (e) {
135166
console.error(`Error installing packages: ${e}`);
136167
process.exit(1);

scripts/demos-use-workspace.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ const linkDemo = async (demoName: string) => {
5252
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
5353

5454
// Track changed files
55-
let changes = 0;
55+
let changes = false;
5656
const updateDeps = (deps: { [key: string]: string }) => {
5757
for (const dep in deps) {
5858
const matchingPackage = workspacePackages.find((p) => p.manifest.name === dep);
5959
if (matchingPackage != undefined && deps[dep] != newPackageVer) {
6060
console.log(`- ${dep}: '${deps[dep]}' => '${newPackageVer}'`);
6161
deps[dep] = newPackageVer;
62-
changes++;
62+
changes = true;
6363
}
6464
}
6565
};
@@ -84,25 +84,35 @@ const linkDemo = async (demoName: string) => {
8484
console.log('- No changes');
8585
}
8686

87-
// Update tsconfig.json using tsconfig.override.json
87+
// Update tsconfig.json using tsconfig.workspace.json
8888
const tsConfigPath = path.join(demoSrc, 'tsconfig.json');
89-
const tsConfigOverridePath = path.join(demoSrc, 'tsconfig.override.json');
89+
const tsConfigWorkspacePath = path.join(demoSrc, 'tsconfig.workspace.json');
9090

91-
if (fs.existsSync(tsConfigOverridePath)) {
91+
if (fs.existsSync(tsConfigWorkspacePath)) {
9292
const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf8'));
93-
const tsConfigOverride = JSON.parse(fs.readFileSync(tsConfigOverridePath, 'utf8'));
9493

95-
console.log('\nApplying tsconfig.json overrides');
94+
const workspaceRef = {
95+
path: 'tsconfig.workspace.json'
96+
};
9697

97-
changes = 0;
98-
for (const entry of Object.entries(tsConfigOverride)) {
99-
const [key, value] = entry;
100-
console.log(`- '${key}'`);
101-
tsConfig[key] = value;
102-
changes++;
98+
console.log('Linking tsconfig.workspace.json');
99+
100+
changes = false;
101+
const references = tsConfig['references'];
102+
103+
if (references) {
104+
const existingWorkspaceRef = references.find((v: any) => v.path === 'tsconfig.workspace.json');
105+
if (existingWorkspaceRef == undefined) {
106+
tsConfig['references'].push(workspaceRef);
107+
changes = true;
108+
}
109+
} else {
110+
tsConfig['references'] = [workspaceRef];
111+
changes = true;
103112
}
104113

105114
if (changes) {
115+
console.log(`- Added '${JSON.stringify(workspaceRef)}'`);
106116
fs.writeFileSync(tsConfigPath, `${JSON.stringify(tsConfig, null, 2)}\n`, 'utf8');
107117
} else {
108118
console.log('- No changes');

0 commit comments

Comments
 (0)