Skip to content

Commit b8f6d23

Browse files
feat: handle disabled lockfile (#107)
1 parent 330d860 commit b8f6d23

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
},
2424
"devDependencies": {
2525
"@playwright/test": "^1.44.1",
26+
"@types/ini": "^4.1.1",
2627
"@types/node": "^18.19.33",
2728
"ansi-colors": "^4.1.1",
2829
"enquirer": "^2.3.6",
2930
"esbuild": "^0.14.25",
31+
"ini": "^4.1.3",
3032
"typescript": "^5.4.5"
3133
}
3234
}

src/generator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import fs from 'fs';
1818

1919
import { prompt } from 'enquirer';
2020
import colors from 'ansi-colors';
21+
import ini from 'ini';
2122

2223
import { executeCommands, createFiles, executeTemplate, Command, languageToFileExtension, getFileExtensionCT } from './utils';
2324
import { type PackageManager, determinePackageManager } from './packageManager';
@@ -174,8 +175,10 @@ export class Generator {
174175
}
175176

176177
if (answers.installGitHubActions) {
178+
const npmrcExists = fs.existsSync(path.join(this.rootDir, '.npmrc'));
179+
const packageLockDisabled = npmrcExists && ini.parse(fs.readFileSync(path.join(this.rootDir, '.npmrc'), 'utf-8'))['package-lock'] === false;
177180
const githubActionsScript = executeTemplate(this._readAsset('github-actions.yml'), {
178-
installDepsCommand: this.packageManager.ci(),
181+
installDepsCommand: packageLockDisabled ? this.packageManager.i() : this.packageManager.ci(),
179182
installPlaywrightCommand: this.packageManager.npx('playwright', 'install --with-deps'),
180183
runTestsCommand: answers.framework ? this.packageManager.run('test-ct') : this.packageManager.runPlaywrightTest(),
181184
}, new Map());

src/packageManager.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface PackageManager {
77
init(): string
88
npx(command: string, args: string): string
99
ci(): string
10+
i(): string
1011
installDevDependency(name: string): string
1112
runPlaywrightTest(args?: string): string
1213
run(script: string): string
@@ -28,6 +29,10 @@ class NPM implements PackageManager {
2829
return 'npm ci'
2930
}
3031

32+
i(): string {
33+
return 'npm i'
34+
}
35+
3136
installDevDependency(name: string): string {
3237
return `npm install --save-dev ${name}`
3338
}
@@ -57,6 +62,10 @@ class Yarn implements PackageManager {
5762
return 'npm install -g yarn && yarn'
5863
}
5964

65+
i(): string {
66+
return this.ci()
67+
}
68+
6069
installDevDependency(name: string): string {
6170
return `yarn add --dev ${name}`
6271
}
@@ -89,6 +98,10 @@ class PNPM implements PackageManager {
8998
return 'npm install -g pnpm && pnpm install'
9099
}
91100

101+
i(): string {
102+
return this.ci()
103+
}
104+
92105
installDevDependency(name: string): string {
93106
return `pnpm add --save-dev ${this.workspace ? '-w ' : ''}${name}`
94107
}

tests/integration.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,26 @@ test('should generate in the root of pnpm workspace', async ({ run, packageManag
101101
expect(fs.existsSync(path.join(dir, 'package.json'))).toBeTruthy();
102102
expect(fs.existsSync(path.join(dir, 'playwright.config.ts'))).toBeTruthy();
103103
});
104+
105+
test('should install with "npm ci" in GHA when using npm with package-lock enabled', async ({ dir, run, packageManager }) => {
106+
test.skip(packageManager !== 'npm');
107+
108+
await run([], { installGitHubActions: true, testDir: 'tests', language: 'JavaScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: true });
109+
expect(fs.existsSync(path.join(dir, '.github/workflows/playwright.yml'))).toBeTruthy();
110+
111+
const workflowContent = fs.readFileSync(path.join(dir, '.github/workflows/playwright.yml'), 'utf8');
112+
expect(workflowContent).not.toContain('run: npm i');
113+
expect(workflowContent).toContain('run: npm ci');
114+
});
115+
116+
test('should install with "npm i" in GHA when using npm with package-lock disabled', async ({ dir, run, packageManager }) => {
117+
test.skip(packageManager !== 'npm');
118+
119+
fs.writeFileSync(path.join(dir, '.npmrc'), 'package-lock=false');
120+
await run([], { installGitHubActions: true, testDir: 'tests', language: 'JavaScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: true });
121+
expect(fs.existsSync(path.join(dir, '.github/workflows/playwright.yml'))).toBeTruthy();
122+
123+
const workflowContent = fs.readFileSync(path.join(dir, '.github/workflows/playwright.yml'), 'utf8');
124+
expect(workflowContent).toContain('run: npm i');
125+
expect(workflowContent).not.toContain('run: npm ci');
126+
});

0 commit comments

Comments
 (0)