From 18db52f456743ba1ea4f9323048fe8da62d9ad8b Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Thu, 10 Jun 2021 11:51:58 +0300 Subject: [PATCH] Build bob by itself --- action/bin.d.ts | 1 + action/index.d.ts | 3 +- bob.config.js | 1 + package.json | 34 +++++++++++-- src/bin.ts | 3 ++ src/commands/build.ts | 23 +++++++++ src/commands/pack-flat.ts | 6 +-- src/index.ts | 4 +- src/rollup-plugins/auto-external.ts | 4 +- tsconfig.json | 7 ++- tsconfig.tsnode.json | 9 ++++ yarn.lock | 79 +++++++++++++++++++++++++++++ 12 files changed, 157 insertions(+), 17 deletions(-) create mode 100644 action/bin.d.ts create mode 100644 bob.config.js create mode 100644 src/bin.ts create mode 100644 tsconfig.tsnode.json diff --git a/action/bin.d.ts b/action/bin.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/action/bin.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/action/index.d.ts b/action/index.d.ts index b7988016..75b8d9a6 100644 --- a/action/index.d.ts +++ b/action/index.d.ts @@ -1,2 +1 @@ -#!/usr/bin/env node -export {}; +export declare function main(): Promise; diff --git a/bob.config.js b/bob.config.js new file mode 100644 index 00000000..f053ebf7 --- /dev/null +++ b/bob.config.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/package.json b/package.json index f401f1b7..2939289b 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,32 @@ "url": "git@github.com:kamilkisiela/bob.git", "type": "git" }, - "main": "dist/index.js", "bin": { - "bob": "dist/index.js" + "bob": "dist/bin.js" + }, + "sideEffects": false, + "main": "dist/index.js", + "module": "dist/index.mjs", + "typings": "dist/index.d.ts", + "typescript": { + "definition": "dist/index.d.ts" + }, + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.mjs" + }, + "./*": { + "require": "./dist/*.js", + "import": "./dist/*.mjs" + } + }, + "buildOptions": { + "bin": { + "bob": { + "input": "src/bin.ts" + } + } }, "license": "MIT", "files": [ @@ -41,8 +64,8 @@ "yargs": "15.3.1" }, "scripts": { - "prepublish": "yarn build", - "build": "tsc && ncc build src/action.ts -o action" + "build": "ts-node --project tsconfig.tsnode.json src/bin.ts build --single && ncc build src/action.ts -o action", + "release": "cd dist && npm publish" }, "devDependencies": { "@actions/core": "1.2.6", @@ -53,7 +76,8 @@ "@types/mkdirp": "1.0.1", "@types/node": "13.11.0", "@types/yargs": "15.0.4", - "typescript": "3.8.3" + "typescript": "3.8.3", + "ts-node": "10.0.0" }, "publishConfig": { "access": "public" diff --git a/src/bin.ts b/src/bin.ts new file mode 100644 index 00000000..e718a0a8 --- /dev/null +++ b/src/bin.ts @@ -0,0 +1,3 @@ +import { main } from "."; + +main(); diff --git a/src/commands/build.ts b/src/commands/build.ts index 25a9939e..66cd213e 100644 --- a/src/commands/build.ts +++ b/src/commands/build.ts @@ -148,6 +148,29 @@ async function buildSingle({ distDir, distPath = '' }: { distDir: string; distPa }) ); + if (buildOptions?.bin) { + await Promise.all( + Object.keys(buildOptions.bin).map(async (alias) => { + const options = buildOptions.bin![alias]; + const binInputOptions = { + ...inputOptions, + ...options, + }; + // create a bundle + const binBundle = await rollup.rollup(binInputOptions); + const targetPath = pkg.bin![alias]; + await binBundle.write({ + ...commonOutputOptions, + banner: `#!/usr/bin/env node`, + preferConst: true, + sourcemap: options.sourcemap, + file: targetPath, + format: "cjs" as const, + }); + }) + ); + } + // move README.md and LICENSE await copyToDist( cwd, diff --git a/src/commands/pack-flat.ts b/src/commands/pack-flat.ts index 51e4b7c6..525ceb6d 100644 --- a/src/commands/pack-flat.ts +++ b/src/commands/pack-flat.ts @@ -1,6 +1,6 @@ import globby from "globby"; import pLimit from "p-limit"; -import fs, { readJSON } from "fs-extra"; +import fs from "fs-extra"; import { resolve, join } from "path"; import { execSync } from "child_process"; import { Consola } from "consola"; @@ -50,7 +50,7 @@ export const packFlatCommand = createCommand< async function pack(packagePath: string, commit: string, config: BobConfig, reporter: Consola) { const cwd = packagePath.replace("/package.json", ""); - const pkg = await readJSON(packagePath); + const pkg = await fs.readJSON(packagePath); const fullName: string = pkg.name; if ((config.ignore || []).includes(fullName)) { @@ -62,7 +62,7 @@ async function pack(packagePath: string, commit: string, config: BobConfig, repo const bobDir = resolve(process.cwd(), ".bob-packed"); // replace version to 0.0.0-canary-${commit} - const distPkg = await readJSON(join(projectDistDir, 'package.json')); + const distPkg = await fs.readJSON(join(projectDistDir, 'package.json')); const version = `0.0.0-canary-${commit}`; distPkg.version = version; await fs.writeFile(join(projectDistDir, 'package.json'), JSON.stringify(distPkg, null, 2), { diff --git a/src/index.ts b/src/index.ts index 09c55ec4..2996c110 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -#!/usr/bin/env node import yargs, { Argv } from "yargs"; import consola from "consola"; import { useConfig } from "./config"; @@ -10,7 +9,7 @@ import { runCommand } from "./commands/run"; import { validateCommand } from "./commands/validate"; import { runifyCommand } from "./commands/runify"; -async function main() { +export async function main() { const config = await useConfig(); const root: Argv = yargs.scriptName("bob").detectLocale(false).version(); @@ -32,4 +31,3 @@ async function main() { .showHelpOnFail(false).argv; } -main(); diff --git a/src/rollup-plugins/auto-external.ts b/src/rollup-plugins/auto-external.ts index c278093e..a4a2ca2f 100644 --- a/src/rollup-plugins/auto-external.ts +++ b/src/rollup-plugins/auto-external.ts @@ -1,5 +1,5 @@ import type { IsExternal, Plugin } from "rollup"; -import { readJSONSync } from "fs-extra"; +import fs from "fs-extra"; import getBuiltins from "builtins"; type Options = { @@ -10,7 +10,7 @@ export function autoExternal({ packageJSONPath }: Options): Plugin { const plugin: Plugin = { name: "auto-external", options(opts) { - const pkg = readJSONSync(packageJSONPath); + const pkg = fs.readJSONSync(packageJSONPath); const externalModules = []; if (typeof pkg.dependencies === "object") { externalModules.push(...Object.keys(pkg.dependencies)); diff --git a/tsconfig.json b/tsconfig.json index 4eb64f17..d9ed82a7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "module": "commonjs", + "module": "esnext", "strictNullChecks": true, "target": "es2018", "lib": ["esnext"], @@ -15,5 +15,8 @@ "noUnusedParameters": true }, "exclude": ["node_modules", "dist"], - "include": ["src/index.ts", "src/typings.d.ts"] + "include": ["src/index.ts", "src/bin.ts", "src/typings.d.ts"], + "ts-node": { + "transpileOnly": true + } } diff --git a/tsconfig.tsnode.json b/tsconfig.tsnode.json new file mode 100644 index 00000000..902ade3e --- /dev/null +++ b/tsconfig.tsnode.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs" + }, + "ts-node": { + "transpileOnly": true + } +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 5a5219ad..3eeb939b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -69,6 +69,26 @@ dependencies: estree-walker "^1.0.1" +"@tsconfig/node10@^1.0.7": + version "1.0.8" + resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.8.tgz#c1e4e80d6f964fbecb3359c43bd48b40f7cadad9" + integrity sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg== + +"@tsconfig/node12@^1.0.7": + version "1.0.8" + resolved "https://registry.yarnpkg.com/@tsconfig/node12/-/node12-1.0.8.tgz#a883d62f049a64fea1e56a6bbe66828d11c6241b" + integrity sha512-LM6XwBhjZRls1qJGpiM/It09SntEwe9M0riXRfQ9s6XlJQG0JPGl92ET18LtGeYh/GuOtafIXqwZeqLOd0FNFQ== + +"@tsconfig/node14@^1.0.0": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.1.tgz#95f2d167ffb9b8d2068b0b235302fafd4df711f2" + integrity sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg== + +"@tsconfig/node16@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.1.tgz#a6ca6a9a0ff366af433f42f5f0e124794ff6b8f1" + integrity sha512-FTgBI767POY/lKNDNbIzgAX6miIDBs6NTCbdlDb8TrWovHsSvaVIZDlTqym29C6UqhzwcJx4CYr+AlrMywA0cA== + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" @@ -171,6 +191,11 @@ ansi-styles@^4.0.0: "@types/color-name" "^1.1.1" color-convert "^2.0.1" +arg@^4.1.0: + version "4.1.3" + resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" + integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA== + array-union@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d" @@ -201,6 +226,11 @@ braces@^3.0.1: dependencies: fill-range "^7.0.1" +buffer-from@^1.0.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef" + integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A== + builtin-modules@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.1.0.tgz#aad97c15131eb76b65b50ef208e7584cd76a7484" @@ -291,6 +321,11 @@ cosmiconfig@6.0.0: path-type "^4.0.0" yaml "^1.7.2" +create-require@^1.1.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" + integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== + cross-spawn@7.0.1: version "7.0.1" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14" @@ -310,6 +345,11 @@ detect-indent@^5.0.0: resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d" integrity sha1-OHHMCmoALow+Wzz38zYmRnXwa50= +diff@^4.0.1: + version "4.0.2" + resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d" + integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A== + dir-glob@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f" @@ -590,6 +630,11 @@ make-dir@^3.0.2: dependencies: semver "^6.0.0" +make-error@^1.1.1: + version "1.3.6" + resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" + integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== + merge2@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.3.0.tgz#5b366ee83b2f1582c48f87e47cf1a9352103ca81" @@ -845,6 +890,19 @@ sort-keys@^2.0.0: dependencies: is-plain-obj "^1.0.0" +source-map-support@^0.5.17: + version "0.5.19" + resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61" + integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw== + dependencies: + buffer-from "^1.0.0" + source-map "^0.6.0" + +source-map@^0.6.0: + version "0.6.1" + resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" + integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== + spdx-correct@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.0.tgz#fb83e504445268f154b074e218c87c003cd31df4" @@ -901,6 +959,22 @@ to-regex-range@^5.0.1: dependencies: is-number "^7.0.0" +ts-node@10.0.0: + version "10.0.0" + resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.0.0.tgz#05f10b9a716b0b624129ad44f0ea05dac84ba3be" + integrity sha512-ROWeOIUvfFbPZkoDis0L/55Fk+6gFQNZwwKPLinacRl6tsxstTF1DbAcLKkovwnpKMVvOMHP1TIbnwXwtLg1gg== + dependencies: + "@tsconfig/node10" "^1.0.7" + "@tsconfig/node12" "^1.0.7" + "@tsconfig/node14" "^1.0.0" + "@tsconfig/node16" "^1.0.1" + arg "^4.1.0" + create-require "^1.1.0" + diff "^4.0.1" + make-error "^1.1.1" + source-map-support "^0.5.17" + yn "3.1.1" + tslib@1.11.1, tslib@^1.11.1: version "1.11.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.11.1.tgz#eb15d128827fbee2841549e171f45ed338ac7e35" @@ -1036,3 +1110,8 @@ yargs@15.3.1: which-module "^2.0.0" y18n "^4.0.0" yargs-parser "^18.1.1" + +yn@3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50" + integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==