1- import * as p from '@clack/prompts' ;
2- import { type AgentName , resolveCommand , parse } from '@sveltejs/sv-utils' ;
31import fs from 'node:fs' ;
42import path from 'node:path' ;
5- import { exec } from 'tinyexec' ;
6- import type { Workspace } from './workspace.ts' ;
3+ import { parseJson } from './tooling/parsers.ts' ;
74
85export type Package = {
96 name : string ;
@@ -27,41 +24,10 @@ export function getPackageJson(cwd: string): {
2724 throw new Error ( `Invalid workspace: missing '${ pkgPath } '` ) ;
2825 }
2926
30- const { data, generateCode } = parse . json ( packageText ) ;
27+ const { data, generateCode } = parseJson ( packageText ) ;
3128 return { source : packageText , data : data as Package , generateCode } ;
3229}
3330
34- export async function formatFiles ( options : {
35- packageManager : AgentName ;
36- cwd : string ;
37- filesToFormat : string [ ] ;
38- } ) : Promise < void > {
39- if ( options . filesToFormat . length === 0 ) return ;
40- const { start, stop } = p . spinner ( ) ;
41- start ( 'Formatting modified files' ) ;
42-
43- const args = [ 'prettier' , '--write' , '--ignore-unknown' , ...options . filesToFormat ] ;
44- const cmd = resolveCommand ( options . packageManager , 'execute-local' , args ) ! ;
45-
46- try {
47- const result = await exec ( cmd . command , cmd . args , {
48- nodeOptions : { cwd : options . cwd , stdio : 'pipe' } ,
49- throwOnError : true
50- } ) ;
51- if ( result . exitCode !== 0 ) {
52- stop ( 'Failed to format files' ) ;
53- p . log . error ( result . stderr ) ;
54- return ;
55- }
56- } catch ( e ) {
57- stop ( 'Failed to format files' ) ;
58- // @ts -expect-error
59- p . log . error ( e ?. output ?. stderr || 'unknown error' ) ;
60- return ;
61- }
62- stop ( 'Successfully formatted modified files' ) ;
63- }
64-
6531export function readFile ( cwd : string , filePath : string ) : string {
6632 const fullFilePath = path . resolve ( cwd , filePath ) ;
6733
@@ -74,11 +40,29 @@ export function readFile(cwd: string, filePath: string): string {
7440 return text ;
7541}
7642
43+ export function fileExists ( cwd : string , filePath : string ) : boolean {
44+ const fullFilePath = path . resolve ( cwd , filePath ) ;
45+ return fs . existsSync ( fullFilePath ) ;
46+ }
47+
48+ export function writeFile ( cwd : string , filePath : string , content : string ) : void {
49+ const fullFilePath = path . resolve ( cwd , filePath ) ;
50+ const fullDirectoryPath = path . dirname ( fullFilePath ) ;
51+
52+ if ( content && ! content . endsWith ( '\n' ) ) content += '\n' ;
53+
54+ if ( ! fs . existsSync ( fullDirectoryPath ) ) {
55+ fs . mkdirSync ( fullDirectoryPath , { recursive : true } ) ;
56+ }
57+
58+ fs . writeFileSync ( fullFilePath , content , 'utf8' ) ;
59+ }
60+
7761export function installPackages (
7862 dependencies : Array < { pkg : string ; version : string ; dev : boolean } > ,
79- workspace : Workspace
63+ cwd : string
8064) : string {
81- const { data, generateCode } = getPackageJson ( workspace . cwd ) ;
65+ const { data, generateCode } = getPackageJson ( cwd ) ;
8266
8367 for ( const dependency of dependencies ) {
8468 if ( dependency . dev ) {
@@ -93,7 +77,7 @@ export function installPackages(
9377 if ( data . dependencies ) data . dependencies = alphabetizeProperties ( data . dependencies ) ;
9478 if ( data . devDependencies ) data . devDependencies = alphabetizeProperties ( data . devDependencies ) ;
9579
96- writeFile ( workspace , commonFilePaths . packageJson , generateCode ( ) ) ;
80+ writeFile ( cwd , commonFilePaths . packageJson , generateCode ( ) ) ;
9781 return commonFilePaths . packageJson ;
9882}
9983
@@ -106,24 +90,6 @@ function alphabetizeProperties(obj: Record<string, string>) {
10690 return orderedObj ;
10791}
10892
109- export function writeFile ( workspace : Workspace , filePath : string , content : string ) : void {
110- const fullFilePath = path . resolve ( workspace . cwd , filePath ) ;
111- const fullDirectoryPath = path . dirname ( fullFilePath ) ;
112-
113- if ( content && ! content . endsWith ( '\n' ) ) content += '\n' ;
114-
115- if ( ! fs . existsSync ( fullDirectoryPath ) ) {
116- fs . mkdirSync ( fullDirectoryPath , { recursive : true } ) ;
117- }
118-
119- fs . writeFileSync ( fullFilePath , content , 'utf8' ) ;
120- }
121-
122- export function fileExists ( cwd : string , filePath : string ) : boolean {
123- const fullFilePath = path . resolve ( cwd , filePath ) ;
124- return fs . existsSync ( fullFilePath ) ;
125- }
126-
12793export const commonFilePaths = {
12894 packageJson : 'package.json' ,
12995 svelteConfig : 'svelte.config.js' ,
0 commit comments