-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add preinstall script for local dev
- Loading branch information
1 parent
f0c8fe6
commit 15280f0
Showing
6 changed files
with
74 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { execa, Options as ExecaOptions } from 'execa'; | ||
|
||
export async function execute(process: string, args: string[], option: ExecaOptions & { dryRun?: boolean }) { | ||
const dryRun = option.dryRun ?? false; | ||
if (dryRun) { | ||
console.log(`[dry-run] ${process} ${args.join(' ')}`); | ||
return; | ||
} | ||
return execa(process, args, option).then(result => { | ||
console.log(result.stdout); | ||
return result; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './execute'; | ||
export * from './version'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
|
||
export async function modifyVersion(packagePath: string, newVersion: string) { | ||
const packageJsonPath = path.resolve(packagePath, 'package.json'); | ||
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8')); | ||
packageJson.version = newVersion; | ||
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)); | ||
} | ||
|
||
export async function modifyPackagesVersion(option: { version: string; directories: string[] }) { | ||
const { version } = option; | ||
for (const directory of option.directories) { | ||
const packages = await fs.readdir(directory); | ||
for (const packageName of packages) { | ||
const packagePath = path.resolve(directory, packageName); | ||
await modifyVersion(packagePath, version); | ||
} | ||
} | ||
} | ||
|
||
export async function modifyAllDependencies(newVersion: string, option: { directories: string[] }) { | ||
const { directories } = option; | ||
for (const directory of directories) { | ||
// In directory, there sub directories which have package.json | ||
const packages = await fs.readdir(directory); | ||
for (const packageName of packages) { | ||
const packagePath = path.resolve(directory, packageName); | ||
await modifyDependency(packagePath, newVersion); | ||
} | ||
} | ||
} | ||
|
||
async function modifyDependency(packagePath: string, newVersion: string) { | ||
const packageJsonPath = path.resolve(packagePath, 'package.json'); | ||
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8')); | ||
const { dependencies } = packageJson; | ||
|
||
// Replaces all @nammatham/* dependencies | ||
for (const [name, version] of Object.entries(dependencies ?? {})) { | ||
if (name.startsWith('@nammatham/')) { | ||
dependencies[name] = newVersion; | ||
} | ||
} | ||
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { modifyAllDependencies } from './libs'; | ||
|
||
async function main() { | ||
await modifyAllDependencies('workspace:*', { directories: ['examples', 'packages'] }); | ||
} | ||
|
||
main().catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters