Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/fishkit/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ export const installWithNpmClient = ({
}
};

export const installSpecifiedDepsWithNpmClient = ({
npmClient,
cwd,
pkg,
flags,
}: {
npmClient: NpmClient;
cwd?: string;
pkg: string;
flags?: string[];
}): void => {
const { NODE_ENV: _, ...env } = process.env;

const flag = flags ?? [];
const args = npmClient === 'yarn' ? ['add', pkg, ...flag] : ['install', pkg, ...flag];

const npm = spawnSync(npmClient, args, {
stdio: 'inherit',
cwd,
env,
});

if (npm.error) {
throw npm.error;
}
};

type Dependencies = Record<string, string>;

interface PackageJson {
Expand Down Expand Up @@ -128,7 +155,32 @@ export class PackageManager {

const npmClient = getNpmClient({ cwd: this.cwd });
await installWithNpmClient({
npmClient
});

console.info(`Dependencies installed with ${npmClient}`);
} catch (error) {
throw new Error(`Failed to install dependencies: ${error}`);
}
}

public installSpecifiedDeps({
pkg,
isDev,
cwd,
}: {
pkg: string;
isDev?: boolean;
cwd?: string;
}): void {
try {
const npmClient = getNpmClient({ cwd: this.cwd });

installSpecifiedDepsWithNpmClient({
npmClient,
cwd,
pkg,
flags: isDev ? ['-D'] : [],
});

console.info(`Dependencies installed with ${npmClient}`);
Expand Down
15 changes: 14 additions & 1 deletion src/sync/write_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'pathe';
import { FRAMEWORK_NAME } from '../constants';
import { writeFileSync } from './fs';
import type { SyncOptions } from './sync';
import { PackageManager } from '../fishkit/npm';

function checkTsconfig(content: string) {
const json = JSON5.parse(content);
Expand All @@ -13,6 +14,15 @@ function checkTsconfig(content: string) {
}
}

function installTypescriptPluginCssModules(context: { cwd: string }) {
const pm = new PackageManager({ cwd: context.cwd });

pm.installSpecifiedDeps({
pkg: 'typescript-plugin-css-modules@^5.1.0',
isDev: true,
});
}

export function generatePathsFromAlias(
targetDir: string,
alias: [string, string][],
Expand Down Expand Up @@ -46,7 +56,7 @@ export function generatePathsFromAlias(
return paths;
}

export function writeTypes({ context }: SyncOptions) {
export async function writeTypes({ context }: SyncOptions) {
const {
paths: { tmpPath },
cwd,
Expand All @@ -57,6 +67,8 @@ export function writeTypes({ context }: SyncOptions) {
const userTsconfigPath = path.join(cwd, 'tsconfig.json');
checkTsconfig(fs.readFileSync(userTsconfigPath, 'utf-8'));

installTypescriptPluginCssModules(context);

const tsconfigPath = path.join(tmpPath, 'tsconfig.json');
const paths = generatePathsFromAlias(tmpPath, config.alias || []);
const tsconfig = {
Expand All @@ -72,6 +84,7 @@ export function writeTypes({ context }: SyncOptions) {
noEmit: true,
strictNullChecks: true,
target: 'esnext',
plugins: [{ name: 'typescript-plugin-css-modules' }],
},
include: [
'client.tsx',
Expand Down