Skip to content

Commit

Permalink
feat(autocomplete-tools): generate index files (#98)
Browse files Browse the repository at this point in the history
* feat(autocomplete-tools): generate index files

* add comment
  • Loading branch information
grant0417 authored Jan 4, 2024
1 parent 1278402 commit b11a5d2
Show file tree
Hide file tree
Showing 16 changed files with 493 additions and 683 deletions.
2 changes: 1 addition & 1 deletion cli/create-completion-spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"devDependencies": {
"@fig/complete-commander": "workspace:^",
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
},
Expand Down
6 changes: 3 additions & 3 deletions cli/publish-spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"homepage": "https://github.com/withfig/autocomplete-tools#readme",
"dependencies": {
"commander": "^11.1.0",
"esbuild": "^0.19.10",
"esbuild": "^0.19.11",
"node-fetch": "^3.3.2",
"prettier": "^3.1.1",
"prompts": "^2.4.2"
Expand All @@ -56,8 +56,8 @@
"@fig/complete-commander": "workspace:^",
"@types/express": "^4.17.21",
"@types/multer": "^1.4.11",
"@types/node": "^20.10.5",
"@types/node-fetch": "^2.6.9",
"@types/node": "^20.10.6",
"@types/node-fetch": "^2.6.10",
"@types/prompts": "^2.4.9",
"express": "^4.18.2",
"multer": "1.4.5-lts.1",
Expand Down
19 changes: 11 additions & 8 deletions cli/tools-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
"description": "Command line tools for working with fig autocomplete specs",
"author": "The Fig Team",
"scripts": {
"build": "rm -rf build/ && tsc",
"build": "rm -rf build/ && pnpm run build:bin && pnpm run build:lib",
"build:bin": "esbuild src/bin.ts --bundle --platform=node --packages=external --outdir=build --minify",
"build:lib": "tsc",
"test": "tsx test/index.ts",
"test:overwrite": "OVERWRITE=true pnpm test",
"prepack": "pnpm build",
"generate-spec": "tsx generate-spec.ts generate-fig-spec"
},
"license": "MIT",
"bin": "./build/bin.js",
"bin": "./build/bin.bundle.js",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
"build/"
],
"dependencies": {
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
"@fig/autocomplete-helpers": "workspace:^",
Expand All @@ -21,20 +28,16 @@
"chokidar": "^3.5.3",
"commander": "^11.1.0",
"create-completion-spec": "workspace:^",
"esbuild": "^0.19.10",
"esbuild": "^0.19.11",
"fast-glob": "^3.3.2",
"module-from-string": "^3.3.0",
"prettier": "^3.1.1",
"semver": "^7.5.4",
"typescript": "^5.3.3"
},
"types": "build/index.d.ts",
"files": [
"build/"
],
"devDependencies": {
"@fig/complete-commander": "workspace:^",
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"@withfig/autocomplete-types": "workspace:^",
"tsx": "^4.7.0"
},
Expand Down
73 changes: 63 additions & 10 deletions cli/tools-cli/src/scripts/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfil
import chokidar from "chokidar";
import { Command } from "commander";
import glob from "fast-glob";
import fs from "node:fs/promises";
import path from "node:path";
import SpecLogger, { Level } from "./log";
import { setSetting } from "./settings";

Expand All @@ -14,22 +16,73 @@ function invalidateCache() {
setSetting("autocomplete.developerModeNPMInvalidateCache", true);
}

/**
* Generate index files for spec build
*/
async function generateIndex(outdir: string, files: string[]) {
const parsedFiles = files.map(path.parse);

const diffVersionedSpecNames = parsedFiles
.filter(({ base }) => base === "index.ts")
.map(({ dir }) => dir.replace(/^src\//, ""));
diffVersionedSpecNames.sort();

const specNames = parsedFiles
.filter(({ dir, ext }) => dir === "src" && ext === ".ts")
.map(({ name }) => name)
.concat(diffVersionedSpecNames);
specNames.sort();

await fs.mkdir(outdir, { recursive: true });

Promise.all([
// index.js
await fs.writeFile(
path.join(outdir, "index.js"),
`var e=${JSON.stringify(specNames)},diffVersionedCompletions=${JSON.stringify(
diffVersionedSpecNames
)};export{e as default,diffVersionedCompletions};`
),
// index.json
fs.writeFile(
path.join(outdir, "index.json"),
JSON.stringify({
completions: specNames,
diffVersionedCompletions: diffVersionedSpecNames,
})
),
// index.d.ts
fs.writeFile(
path.join(outdir, "index.d.ts"),
`declare const completions: string[]
declare const diffVersionedCompletions: string[]
export { completions as default, diffVersionedCompletions }
`
),
]);
}

/**
* Transpiles all passed files and prints the progress
* @param specs Array of filepaths
*/
async function processFiles(files: string[], isDev?: boolean, outdir?: string) {
const fileName = files.length === 1 ? files[0] : `${files.length} specs`;
await build({
entryPoints: files,
outdir: outdir ?? DEFAULT_DESTINATION_FOLDER_NAME,
bundle: true,
outbase: "src",
format: "esm",
minify: true,
plugins: [NodeModulesPolyfillPlugin()],
...(isDev && { sourcemap: "inline" }),
}).catch((e) => SpecLogger.log(`Error building ${fileName}: ${e.message}`, Level.ERROR));

await Promise.all([
build({
entryPoints: files,
outdir: outdir ?? DEFAULT_DESTINATION_FOLDER_NAME,
bundle: true,
outbase: "src",
format: "esm",
minify: true,
plugins: [NodeModulesPolyfillPlugin()],
...(isDev && { sourcemap: "inline" }),
}).catch((e) => SpecLogger.log(`Error building ${fileName}: ${e.message}`, Level.ERROR)),
generateIndex(outdir ?? DEFAULT_DESTINATION_FOLDER_NAME, files),
]);

SpecLogger.log(`Built ${fileName}`);
invalidateCache();
}
Expand Down
15 changes: 8 additions & 7 deletions cli/tools-cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es2015", "DOM"],
"target": "ESNext",
"lib": ["ESNext", "DOM"],
"allowJs": false,
"skipLibCheck": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"moduleResolution": "node",
"module": "ESNext",
"moduleResolution": "Bundler",
"rootDir": "src",
"outDir": "build/",
"types": ["@withfig/autocomplete-types", "node"]
"outDir": "build",
"types": ["@withfig/autocomplete-types", "node"],
"declaration": true
},
"exclude": ["node_modules/", "generate-spec.ts", "test"]
"exclude": ["node_modules/", "generate-spec.ts", "test", "src/bin.ts"]
}
4 changes: 2 additions & 2 deletions eslint/config-autocomplete/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"license": "MIT",
"main": "./index.js",
"dependencies": {
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@withfig/eslint-plugin-fig-linter": "workspace:^",
"eslint-plugin-compat": "^4.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion generators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"@types/sinon-chai": "^3.2.12",
"@withfig/autocomplete-types": "workspace:^",
"chai": "^4.3.10",
Expand Down
2 changes: 1 addition & 1 deletion helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"@tsconfig/recommended": "^1.0.3",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"@types/semver": "^7.5.6",
"@withfig/autocomplete-types": "workspace:^",
"jest": "^29.7.0",
Expand Down
2 changes: 1 addition & 1 deletion hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.45",
"@types/react": "^18.2.46",
"@withfig/autocomplete-types": "workspace:^",
"typescript": "^5.3.3"
},
Expand Down
2 changes: 1 addition & 1 deletion integrations/commander/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prettier": "^3.1.1"
},
"devDependencies": {
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"@withfig/autocomplete-types": "workspace:^",
"chalk": "^5.3.0",
"commander": "^11.1.0",
Expand Down
6 changes: 3 additions & 3 deletions integrations/oclif/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"author": "Matt Schrage @mattschrage",
"bugs": "https://github.com/withfig/autocomplete-tools/issues",
"dependencies": {
"@oclif/core": "^3.15.0",
"@oclif/core": "^3.16.0",
"prettier": "^3.1.1"
},
"devDependencies": {
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"@withfig/autocomplete-types": "workspace:^",
"oclif": "^4.1.0",
"oclif": "^4.1.3",
"tslib": "^2.6.2",
"typescript": "^5.3.3"
},
Expand Down
2 changes: 1 addition & 1 deletion merge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lib/"
],
"devDependencies": {
"@types/node": "^20.10.5",
"@types/node": "^20.10.6",
"tsx": "^4.7.0"
},
"fig": {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"name": "root",
"private": true,
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.56.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.0",
"eslint-plugin-prettier": "^5.1.2",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"prettier": "3.1.0",
"turbo": "^1.11.2"
"turbo": "^1.11.3"
},
"scripts": {
"lint": "eslint .",
Expand Down
Loading

0 comments on commit b11a5d2

Please sign in to comment.