Skip to content

Commit

Permalink
feat: add preinstall script for local dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Dec 30, 2023
1 parent f0c8fe6 commit 15280f0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 55 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"format": "prettier --write '**/*.ts'",
"dev": "nx run @nammatham/core:build && nx run-many -t dev",
"preinstall": "tsx ./scripts/preinstall.ts",
"release": "run-s build releaseOnly",
"releaseOnly": "tsx ./scripts/release.ts",
"build": "nx run-many -t build",
Expand Down
13 changes: 13 additions & 0 deletions scripts/libs/execute.ts
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;
});
}
2 changes: 2 additions & 0 deletions scripts/libs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './execute';
export * from './version';
46 changes: 46 additions & 0 deletions scripts/libs/version.ts
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));
}
10 changes: 10 additions & 0 deletions scripts/preinstall.ts
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);
});
57 changes: 2 additions & 55 deletions scripts/release.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { execa, Options as ExecaOptions } from 'execa';

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;
});
}
import { execute, modifyAllDependencies, modifyPackagesVersion, modifyVersion } from './libs';

export type ReleaseType = 'major' | 'minor' | 'patch' | 'alpha';

Expand All @@ -26,7 +14,7 @@ async function main() {
const newVersion = bumpVersion(version, { dryRun, releaseType });
await modifyAllDependencies(newVersion, { directories: ['examples', 'packages'] });
await modifyVersion(process.cwd(), newVersion);
await modifyPublishPackagesVersion({ version: newVersion, directory: path.resolve('packages') });
await modifyPackagesVersion({ version: newVersion, directories: [path.resolve('packages')] });
await execute('git', ['add', '.'], { dryRun });
await execute('git', ['commit', '-m', `Bump version v${newVersion}`], { dryRun });
await publishPackages({
Expand All @@ -39,41 +27,7 @@ async function main() {
await execute('git', ['push', 'origin', '--tags'], { dryRun });
}

async function modifyPublishPackagesVersion(option: { version: string; directory: string }) {
const { version, directory } = option;
const packages = await fs.readdir(directory);
for (const packageName of packages) {
const packagePath = path.resolve(directory, packageName);
await modifyVersion(packagePath, version);
}
}

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);
const { name } = await readPackageJson(packagePath);
await modifyDependency(packagePath, name, newVersion);
}
}
}

async function modifyDependency(packagePath: string, dependencyName: 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));
}

export interface PublishPackagesOptions {
version: string;
Expand Down Expand Up @@ -110,13 +64,6 @@ function bumpVersion(version: string, option: { dryRun?: boolean; releaseType: R
}
}

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));
}

async function readPackageJson(packagePath: string) {
const packageJsonPath = path.resolve(packagePath, 'package.json');
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8'));
Expand Down

0 comments on commit 15280f0

Please sign in to comment.