Skip to content

Commit 1eec4e6

Browse files
authored
feat: add pnpm support (#191)
1 parent 399cbe3 commit 1eec4e6

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@
1212
"**/.yarn": true,
1313
"**/dist/": true,
1414
"**/.git/": true
15+
},
16+
"files.associations": {
17+
"*.js.sapphire": "javascript",
18+
"*.ts.sapphire": "typescript",
19+
"*.json.sapphire": "json",
20+
"*.yml.sapphire": "yaml"
1521
}
1622
}

src/commands/new.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function editPackageJson(location: string, name: string) {
2323
return result.isOk();
2424
}
2525

26-
async function installDeps(location: string, pm: string, verbose: boolean) {
26+
async function installDeps(location: string, pm: 'npm' | 'Yarn' | 'pnpm', verbose: boolean) {
2727
const value = await execa(pm.toLowerCase(), ['install'], {
2828
stdio: verbose ? 'inherit' : undefined,
2929
cwd: `./${location}/`
@@ -33,10 +33,18 @@ async function installDeps(location: string, pm: string, verbose: boolean) {
3333
throw new Error('An unknown error occurred while installing the dependencies. Try running Sapphire CLI with "--verbose" flag.');
3434
}
3535

36-
const oppositeLockfile = `./${location}/${pm === 'npm' ? 'yarn.lock' : 'package-lock.json'}`;
36+
const oppositeLockfiles = {
37+
npm: ['yarn.lock', 'pnpm-lock.yaml'],
38+
yarn: ['package-lock.json', 'pnpm-lock.yaml'],
39+
pnpm: ['package-lock.json', 'yarn.lock']
40+
} as const;
3741

38-
if (await fileExists(oppositeLockfile)) {
39-
await rm(oppositeLockfile);
42+
const lockfiles = pm === 'npm' ? oppositeLockfiles.npm : pm === 'Yarn' ? oppositeLockfiles.yarn : oppositeLockfiles.pnpm;
43+
44+
for (const lockfile of lockfiles) {
45+
if (await fileExists(lockfile)) {
46+
await rm(lockfile);
47+
}
4048
}
4149

4250
return true;
@@ -103,7 +111,7 @@ async function cloneRepo(location: string, verbose: boolean) {
103111
}
104112

105113
export default async (name: string, flags: Record<string, boolean>) => {
106-
const response = await prompts<PromptNewObjectKeys>(PromptNew(name, await CommandExists('yarn')));
114+
const response = await prompts<PromptNewObjectKeys>(PromptNew(name, await CommandExists('yarn'), await CommandExists('pnpm')));
107115

108116
if (!response.projectName || !response.projectLang || !response.projectTemplate || !response.packageManager) {
109117
process.exit(1);
@@ -130,6 +138,10 @@ export default async (name: string, flags: Record<string, boolean>) => {
130138
);
131139

132140
await editPackageJson(response.projectName, projectName);
141+
142+
if (response.packageManager === 'pnpm') {
143+
await writeFile(`./${response.projectName}/.npmrc`, '# pnpm only\nshamefully-hoist=true\npublic-hoist-pattern[]=@sapphire/*');
144+
}
133145
};
134146

135147
const jobs: [() => any, string][] = [

src/prompts/PromptNew.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ const jsTemplates: Choice[] = [
1212
{ title: 'with CommonJS', value: 'with-javascript' }
1313
];
1414

15-
export const PromptNew = (projectName: string, yarn: boolean): PromptObject<PromptNewObjectKeys>[] => {
15+
export const PromptNew = (projectName: string, yarn: boolean, pnpm: boolean): PromptObject<PromptNewObjectKeys>[] => {
1616
const pmChoices = [
1717
{
1818
title: `Yarn (Recommended) ${yarn ? '' : '(Not installed)'}`,
1919
value: 'Yarn',
2020
disabled: !yarn
2121
},
22+
{
23+
title: `pnpm ${pnpm ? '' : '(Not Installed)'}`,
24+
value: 'pnpm',
25+
disabled: !pnpm
26+
},
2227
{ title: 'npm', value: 'npm' }
2328
];
2429

0 commit comments

Comments
 (0)