Skip to content

Commit

Permalink
Merge pull request #3 from takuma-ru/release/2024_08_06T03_02_07_307Z
Browse files Browse the repository at this point in the history
Release/2024 08 06 t03 02 07 307 z
  • Loading branch information
takuma-ru authored Aug 6, 2024
2 parents 9d79c7e + 9af7941 commit 1c84f85
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 33 deletions.
5 changes: 4 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"module": "./dist/main.js",
"types": "./dist/main.d.ts",
"bin": {
"rlse": "./bin/main.cjs"
"rlse": "./bin/bin.cjs"
},
"exports": {
".": {
Expand Down Expand Up @@ -59,5 +59,8 @@
"tsup": "^8.1.0",
"typescript": "^5.2.2",
"zod": "^3.23.8"
},
"dependencies": {
"ts-node": "^10.9.2"
}
}
7 changes: 6 additions & 1 deletion packages/core/src/action/releaseAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export const releaseAction = async (options: unknown) => {
consola.error(error.errors);
process.exit(1);
}
const { name, pre, level, buildCmd, dryRun } = data;
const { name, pre, level, buildCmd, dryRun, noRun } = data;

if (noRun) {
consola.info("No run");
process.exit(0);
}

// == Package info ==
const packageJsonPath = await findPackageJsonByName(name);
Expand Down
25 changes: 25 additions & 0 deletions packages/core/src/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Command } from "commander";

import consola from "consola";
import { releaseAction } from "./action/releaseAction";
import { loadRlseConfig } from "./config/loadRlseConfig";

const program = new Command();

program
.name("rlse")
.description("Release npm package")
.option("-n, --name <name>", "package name")
.option("--pre", "Release new pre-release")
.option("-l, --level <patch | minor | major | pre>", "release level")
.option("-c, --build-cmd <cmd>", "build command")
.option("--dry-run", "dry run")
.option("--no-run", "no run")
.action(async (options) => {
const optionFromFile = await loadRlseConfig();
consola.info("optionFromFile", optionFromFile);
const mergedOptions = { ...optionFromFile, ...options };
releaseAction(mergedOptions);
});

program.parse();
5 changes: 5 additions & 0 deletions packages/core/src/config/defineConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { RlseConfig } from "../types/RlseConfig";

export const defineConfig = (config: RlseConfig) => {
return config;
};
98 changes: 98 additions & 0 deletions packages/core/src/config/loadRlseConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { execSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { cwd } from "node:process";
import { promisify } from "node:util";

const readFile = promisify(fs.readFile);
const configFiles = [
"rlse.config.ts",
"rlse.config.js",
"rlse.config.mjs",
"rlse.config.cjs",
"rlse.config.json",
];

export const loadRlseConfig = async () => {
for (const file of configFiles) {
const filePath = path.resolve(cwd(), file);
if (fs.existsSync(filePath)) {
const ext = path.extname(file);
switch (ext) {
case ".ts":
// TypeScriptファイルの処理
return await importTypeScriptConfig(filePath);
case ".js":
case ".mjs":
case ".cjs":
// JavaScriptファイルの処理
return require(filePath);
case ".json": {
// JSONファイルの処理
const content = await readFile(filePath, "utf-8");
return JSON.parse(content);
}
default:
throw new Error(`Unsupported file extension: ${ext}`);
}
}
}
throw new Error("No configuration file found");
};

const importTypeScriptConfig = async (filePath: string) => {
const tempDir = path.resolve(cwd(), ".temp");
const tempFilePath = path.resolve(tempDir, "rlse.config.js");
const customTsConfigPath = path.resolve(tempDir, "tsconfig.json");

// 一時ディレクトリが存在しない場合は作成する
try {
fs.mkdirSync(tempDir, { recursive: true });
} catch (error) {
console.error("Error creating temporary directory:", error);
throw error;
}

// カスタムのtsconfig.jsonを作成
fs.writeFileSync(
customTsConfigPath,
JSON.stringify({
compilerOptions: {
target: "ES2022",
useDefineForClassFields: true,
module: "ESNext",
lib: ["ESNext", "DOM", "DOM.Iterable", "ES2022"],
skipLibCheck: true,
noErrorTruncation: true,

/* Bundler mode */
moduleResolution: "bundler",
allowImportingTsExtensions: true,
resolveJsonModule: true,
isolatedModules: true,
emitDeclarationOnly: true,
declaration: true,
declarationMap: true,
outDir: tempDir,
rootDir: path.dirname(filePath),

/* Linting */
strict: true,
noUnusedLocals: true,
noUnusedParameters: true,
noFallthroughCasesInSwitch: true,
},
exclude: ["node_modules"],
include: [filePath],
})
);

try {
execSync(`tsc --project ${customTsConfigPath}`, { stdio: "inherit" });
const config = await import(tempFilePath);
return config.default;
} catch (error) {
console.error("Error compiling TypeScript file:", error);
throw error;
}
};
19 changes: 2 additions & 17 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import { Command } from "commander";
import { releaseAction } from "./action/releaseAction";
import { defineConfig } from "./config/defineConfig";

const program = new Command();

program
.name("rlse")
.description("Release npm package")
.option("-n, --name <name>", "package name")
.option("--pre", "Release new pre-release")
.option("-l, --level <patch | minor | major | pre>", "release level")
.option("-c, --build-cmd <cmd>", "build command")
.option("--dry-run", "dry run")
.action(async (options) => {
releaseAction(options);
});

program.parse();
export { defineConfig };
11 changes: 10 additions & 1 deletion packages/core/src/types/RlseConfig.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
// export type RlseConfig = {};
import type { ReleaseSchemaType } from "../validation/validation";

export type RlseConfig = ReleaseSchemaType & {
/**
* Stop Release Flow
*
* @default false
*/
isStopFlow: boolean;
};
1 change: 1 addition & 0 deletions packages/core/src/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const releaseSchema = z.object({
message: "Invalid build command",
}),
dryRun: z.boolean().optional().default(false),
noRun: z.boolean().optional().default(false),
});

export const parseReleaseSchema = (options: unknown) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { mkdir, readFileSync, writeFile } from "fs";
import path from "path";
import { cwd } from "process";
import { mkdir, readFileSync, writeFile } from "node:fs";
import path from "node:path";
import { cwd } from "node:process";
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/main.ts"],
entry: ["src/main.ts", "src/bin.ts"],
format: ["esm", "cjs"],
splitting: false,
sourcemap: true,
Expand All @@ -19,7 +19,7 @@ export default defineConfig({
}
});

const filesToCopy = ["dist/main.cjs", "dist/main.js"];
const filesToCopy = ["dist/bin.cjs", "dist/bin.js"];

for (const file of filesToCopy) {
const content = await readFileSync(file, { encoding: "utf8" });
Expand Down
7 changes: 4 additions & 3 deletions playgrounds/vanilla-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "vanilla-ts",
"private": true,
"version": "0.0.1",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"link:core": "pnpm link ../../packages/core",
"rlse:test": "pnpm link:core && rlse -n vanilla-ts -c \"pnpm build\" -l patch --dry-run"
"rlse:test": "pnpm link:core && rlse -n vanilla-ts -c \"pnpm build\" -l patch --dry-run --no-run"
},
"devDependencies": {
"@takuma-ru/rlse": "workspace:*",
"ts-node": "^10.9.2",
"typescript": "^5.2.2",
"vite": "^5.3.5"
}
}
}
11 changes: 11 additions & 0 deletions playgrounds/vanilla-ts/rlse.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "@takuma-ru/rlse";

export default defineConfig({
isStopFlow: false,
name: "vanilla-ts",
level: "patch",
pre: false,
buildCmd: "pnpm build",
dryRun: true,
noRun: true,
});
2 changes: 1 addition & 1 deletion playgrounds/vanilla-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
"include": ["src", "rlse.config.ts"]
}
Loading

0 comments on commit 1c84f85

Please sign in to comment.