diff options
Diffstat (limited to 'cli/scripts')
| -rw-r--r-- | cli/scripts/generate.mjs | 297 | ||||
| -rw-r--r-- | cli/scripts/nginx/.dockerignore | 1 | ||||
| -rw-r--r-- | cli/scripts/nginx/Dockerfile.cacher | 11 | ||||
| -rw-r--r-- | cli/scripts/nginx/docker-compose.yml | 9 | ||||
| -rw-r--r-- | cli/scripts/nginx/nginx.conf | 39 | ||||
| -rw-r--r-- | cli/scripts/npm-native-packages/.gitignore | 1 | ||||
| -rw-r--r-- | cli/scripts/npm-native-packages/npm-native-packages.js | 57 | ||||
| -rw-r--r-- | cli/scripts/npm-native-packages/template/README.md | 3 | ||||
| -rw-r--r-- | cli/scripts/npm-native-packages/template/bin/turbo | 15 | ||||
| -rw-r--r-- | cli/scripts/npm-native-packages/template/template.package.json | 12 | ||||
| -rw-r--r-- | cli/scripts/templates/jest.config.js | 10 | ||||
| -rw-r--r-- | cli/scripts/templates/src/__tests__/index.test.ts | 7 | ||||
| -rw-r--r-- | cli/scripts/templates/src/__tests__/tsconfig.json | 4 | ||||
| -rw-r--r-- | cli/scripts/templates/src/index.ts | 3 | ||||
| -rw-r--r-- | cli/scripts/templates/tsconfig.json | 9 |
15 files changed, 478 insertions, 0 deletions
diff --git a/cli/scripts/generate.mjs b/cli/scripts/generate.mjs new file mode 100644 index 0000000..1b9cbec --- /dev/null +++ b/cli/scripts/generate.mjs @@ -0,0 +1,297 @@ +#!/usr/bin/env node +import shelljs from "shelljs"; +import path from "path"; +import fs from "fs-extra"; +import faker from "faker"; +import graphGenerator from "ngraph.generators"; +import copy from "copy-template-dir"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +faker.seed(123); + +const scope = `@${faker.hacker + .noun() + .toLowerCase() + .replace(/\s/g, "-") + .replace("1080p-", "rando")}`; + +const type = process.argv[2]; + +// TODO: algo should be customizable along with the size +const packageGraph = graphGenerator.complete(5); + +// Generate the package name & versions +packageGraph.forEachNode((node) => { + node.data = { + name: `${scope}/${faker.hacker.adjective()}-${faker.hacker.noun()}` + .toLocaleLowerCase() + .replace(/\s/g, "-"), + version: faker.system.semver(), + }; +}); + +// Generate package dependencies +packageGraph.forEachNode((node) => { + const links = packageGraph.getLinks(node.id); + + if (links) { + for (const link of links) { + if (link.fromId === node.id) { + const depNode = packageGraph.getNode(link.toId); + node.data.dependencies = node.data.dependencies || {}; + node.data.dependencies[depNode.data.name] = `^${depNode.data.version}`; + node.data.implicitDependencies = node.data.implicitDependencies || []; + node.data.implicitDependencies.push( + depNode.data.name.replace(/^@[^/]+\//, "") + ); + } + } + } +}); + +// Generate the monorepo +// 1. the root package.json +// 2. create packages/ +// 3. create package directories +const root = path.join(__dirname, "../demo", type); + +function generate(root, skipInstall) { + fs.mkdirSync(root, { recursive: true }); + if (type !== "nx") { + fs.writeFileSync( + path.join(root, ".gitignore"), + `node_modules +dist +.turbo +out +turbo +turbo-linux +.yalc` + ); + if (fs.existsSync(root)) { + try { + fs.rmSync(root + "/packages", { recursive: true }); + } catch (error) {} + } + + let deps = + type !== "turbo" + ? { + devDependencies: { + [type]: "*", + }, + } + : {}; + + fs.writeFileSync( + path.join(root, "package.json"), + JSON.stringify( + { + name: "monorepo", + version: "0.0.0", + private: true, + workspaces: ["packages/*"], + ...deps, + packageManager: "yarn@1.22.17", + }, + null, + 2 + ) + ); + + fs.writeFileSync( + path.join(root, "tsconfig.json"), + JSON.stringify( + { + compilerOptions: { + composite: false, + declaration: true, + declarationMap: true, + esModuleInterop: true, + forceConsistentCasingInFileNames: true, + inlineSourceMap: true, + inlineSources: false, + isolatedModules: true, + moduleResolution: "node", + noUnusedLocals: false, + noUnusedParameters: false, + preserveWatchOutput: true, + skipLibCheck: true, + strict: true, + lib: ["es2020"], + module: "commonjs", + target: "es2020", + }, + }, + null, + 2 + ) + ); + } + + if (type === "turbo") { + fs.writeFileSync( + path.join(root, "turbo.json"), + JSON.stringify( + { + npmClient: "yarn", + cacheStorageConfig: { + provider: "local", + cacheUrl: "https://1a77600385dd.ngrok.io", + }, + pipeline: { + build: { + outputs: ["dist/**/*"], + dependsOn: ["^build"], + }, + test: { + dependsOn: ["build"], + }, + dev: { + cache: false, + }, + }, + }, + null, + 2 + ) + ); + } + + if (type === "lerna") { + fs.writeFileSync( + path.join(root, "lerna.json"), + JSON.stringify( + { + packages: ["packages/*"], + version: "0.0.0", + }, + null, + 2 + ) + ); + } + + if (type === "lage") { + fs.writeFileSync( + path.join(root, "lage.config.js"), + ` +module.exports = { + pipeline: { + build: ['^build'], + test: ['build'], + lint: [], + }, + npmClient: 'yarn', + cacheOptions: { + cacheStorageConfig: { + provider: 'local', + }, + outputGlob: ['dist/**'], + }, +}; + ` + ); + } + + if (type !== "nx") { + fs.mkdirSync(path.join(root, "packages")); + } else { + shelljs.exec( + `cd ${path.join( + __dirname, + "../demo" + )} && yarn create nx-workspace nx --preset=empty --nx-cloud=false --packageManager=yarn --cli=nx --linter=eslint` + ); + shelljs.exec(`cd ${root} && yarn add @nrwl/node`); + } + + if (type !== "nx") { + packageGraph.forEachNode((node) => { + const packageRoot = path.join( + root, + "packages", + node.data.name.replace(/^@[^/]+\//, "") + ); + fs.mkdirSync(packageRoot, { recursive: true }); + copy( + path.join(__dirname, "templates"), + path.join(packageRoot), + { + name: node.data.name.replace(/^@[^/]+\//, ""), + }, + () => {} + ); + + fs.writeFileSync( + path.join(packageRoot, "package.json"), + JSON.stringify( + { + name: node.data.name, + version: node.data.version, + dependencies: node.data.dependencies, + files: ["dist/**"], + main: "dist/index.js", + types: "dist/index.d.ts", + devDependencies: { + typescript: "^4.6.3", + jest: "^27.0.0", + "ts-jest": "^27.0.0", + "@types/jest": "^27.0.0", + }, + scripts: { + build: "tsc", + dev: "tsc -w", + test: "jest", + }, + }, + null, + 2 + ) + ); + }); + } + + if (type === "nx") { + packageGraph.forEachNode((node) => { + shelljs.exec( + `cd ${root} && yarn nx g @nrwl/node:library --buildable --publishable --name="${node.data.name.replace( + /^@[^/]+\//, + "" + )}" --importPath="${node.data.name.replace(/^@[^/]+\//, "")}"` + ); + // instead of dealing with actual code, just list as implicitDependencies + const safeName = node.data.name.replace(/^@[^/]+\//, ""); + const workspace = fs.readJSONSync(path.join(root, "workspace.json")); + workspace.projects[safeName] = { + ...workspace.projects[safeName], + implicitDependencies: node.data.implicitDependencies || [], + }; + fs.writeFileSync( + path.join(root, "nx.json"), + JSON.stringify(workspace, null, 2) + ); + }); + } + if (!skipInstall) { + shelljs.exec(`cd ${root} && yarn install`); + } + fs.ensureDirSync(path.join(root, ".git")); + fs.writeFileSync( + path.join(root, ".git", "config"), + ` +[user] + name = GitHub Actions + email = actions@users.noreply.github.com +` + ); + shelljs.exec( + `cd ${root} && git init -q && git add . && git commit -m "init"` + ); +} + +generate(root); +if (type === "turbo") { + generate(root + "-installed", true); +} diff --git a/cli/scripts/nginx/.dockerignore b/cli/scripts/nginx/.dockerignore new file mode 100644 index 0000000..4c8fbef --- /dev/null +++ b/cli/scripts/nginx/.dockerignore @@ -0,0 +1 @@ +cacher_root diff --git a/cli/scripts/nginx/Dockerfile.cacher b/cli/scripts/nginx/Dockerfile.cacher new file mode 100644 index 0000000..aedf629 --- /dev/null +++ b/cli/scripts/nginx/Dockerfile.cacher @@ -0,0 +1,11 @@ +FROM ubuntu:xenial + +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + nginx \ + nginx-extras \ + && rm -rf /var/lib/apt/lists/* + +COPY nginx.conf /etc/nginx/nginx.conf + +CMD nginx -g "daemon off;" diff --git a/cli/scripts/nginx/docker-compose.yml b/cli/scripts/nginx/docker-compose.yml new file mode 100644 index 0000000..d93ef16 --- /dev/null +++ b/cli/scripts/nginx/docker-compose.yml @@ -0,0 +1,9 @@ +services: + cacher: + build: + context: . + dockerfile: Dockerfile.cacher + volumes: + - ./cacher_root:/var/www/cache + ports: + - "7070:7070" diff --git a/cli/scripts/nginx/nginx.conf b/cli/scripts/nginx/nginx.conf new file mode 100644 index 0000000..d56b5c0 --- /dev/null +++ b/cli/scripts/nginx/nginx.conf @@ -0,0 +1,39 @@ +user root; +worker_processes auto; +pid /run/nginx.pid; + +events { + worker_connections 768; + # multi_accept on; +} + +http { + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + # server_tokens off; + + include /etc/nginx/mime.types; + default_type application/octet-stream; + + access_log /dev/stdout; + error_log /dev/stderr; + + gzip on; + gzip_disable "msie6"; + + server { + listen 7070 default_server; + + root /var/www; + + location /v8/artifacts { + dav_methods PUT; + autoindex on; + allow all; + client_max_body_size 512M; + } + } +} diff --git a/cli/scripts/npm-native-packages/.gitignore b/cli/scripts/npm-native-packages/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/cli/scripts/npm-native-packages/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/cli/scripts/npm-native-packages/npm-native-packages.js b/cli/scripts/npm-native-packages/npm-native-packages.js new file mode 100644 index 0000000..06ab67f --- /dev/null +++ b/cli/scripts/npm-native-packages/npm-native-packages.js @@ -0,0 +1,57 @@ +#!/usr/bin/env node + +const fs = require("fs"); +const path = require("path"); + +// Map to node os and arch names. +const nodeOsLookup = { + darwin: "darwin", + linux: "linux", + windows: "win32", +}; + +const nodeArchLookup = { + amd64: "x64", + arm64: "arm64", +}; + +const humanizedArchLookup = { + amd64: "64", + arm64: "arm64", +}; + +const template = require("./template/template.package.json"); +const os = process.argv[2]; +const arch = process.argv[3]; +const version = process.argv[4]; + +template.name = `turbo-${os}-${humanizedArchLookup[arch]}`; +template.description = `The ${os}-${humanizedArchLookup[arch]} binary for turbo, a monorepo build system.`; +template.os = [nodeOsLookup[os]]; +template.cpu = [nodeArchLookup[arch]]; +template.version = version; + +const outputPath = path.join(__dirname, "build", template.name); +fs.rmSync(outputPath, { recursive: true, force: true }); +fs.mkdirSync(path.join(outputPath, "bin"), { recursive: true }); + +if (os === "windows") { + fs.copyFileSync( + path.join(__dirname, "template", "bin", "turbo"), + path.join(outputPath, "bin", "turbo") + ); +} +fs.copyFileSync( + path.join(__dirname, "template", "README.md"), + path.join(outputPath, "README.md") +); +fs.writeFileSync( + path.join(outputPath, "package.json"), + JSON.stringify(template, null, 2) +); + +const goBin = os === "windows" ? "go-turbo.exe" : "go-turbo"; +fs.copyFileSync( + path.join(__dirname, "..", "..", `dist-${os}-${arch}`, goBin), + path.join(outputPath, "bin", goBin) +); diff --git a/cli/scripts/npm-native-packages/template/README.md b/cli/scripts/npm-native-packages/template/README.md new file mode 100644 index 0000000..fcbd4c0 --- /dev/null +++ b/cli/scripts/npm-native-packages/template/README.md @@ -0,0 +1,3 @@ +# `turbo` + +This is a platform-specific binary for Turborepo, a monorepo build system. See https://github.com/vercel/turbo for details. diff --git a/cli/scripts/npm-native-packages/template/bin/turbo b/cli/scripts/npm-native-packages/template/bin/turbo new file mode 100644 index 0000000..4557a07 --- /dev/null +++ b/cli/scripts/npm-native-packages/template/bin/turbo @@ -0,0 +1,15 @@ +#!/usr/bin/env node + +// Unfortunately even though npm shims "bin" commands on Windows with auto- +// generated forwarding scripts, it doesn't strip the ".exe" from the file name +// first. So it's possible to publish executables via npm on all platforms +// except Windows. I consider this a npm bug. +// +// My workaround is to add this script as another layer of indirection. It'll +// be slower because node has to boot up just to shell out to the actual exe, +// but Windows is somewhat of a second-class platform to npm so it's the best +// I can do I think. +const path = require('path'); +const turbo_exe = path.join(__dirname, 'turbo.exe'); +const child_process = require('child_process'); +child_process.spawnSync(turbo_exe, process.argv.slice(2), { stdio: 'inherit' }); diff --git a/cli/scripts/npm-native-packages/template/template.package.json b/cli/scripts/npm-native-packages/template/template.package.json new file mode 100644 index 0000000..fdc72c0 --- /dev/null +++ b/cli/scripts/npm-native-packages/template/template.package.json @@ -0,0 +1,12 @@ +{ + "name": "turbo-{{Os}}-{{Arch}}", + "version": "{{Version}", + "description": "The {{Os}}-{{Arch}} binary for turbo, a monorepo build system.", + "repository": "https://github.com/vercel/turbo", + "bugs": "https://github.com/vercel/turbo/issues", + "homepage": "https://turbo.build/repo", + "license": "MPL-2.0", + "os": ["{{Os}}"], + "cpu": ["{{Arch}}"], + "preferUnplugged": true +} diff --git a/cli/scripts/templates/jest.config.js b/cli/scripts/templates/jest.config.js new file mode 100644 index 0000000..0548306 --- /dev/null +++ b/cli/scripts/templates/jest.config.js @@ -0,0 +1,10 @@ +module.exports = { + roots: ["<rootDir>/src"], + transform: { + "^.+\\.tsx?$": "ts-jest", + }, + // testRegex: '(/__tests__/.*(\\.|/)(test|spec))\\.tsx?$', + moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], + modulePathIgnorePatterns: ["<rootDir>/src/__fixtures__"], + preset: "ts-jest", +}; diff --git a/cli/scripts/templates/src/__tests__/index.test.ts b/cli/scripts/templates/src/__tests__/index.test.ts new file mode 100644 index 0000000..9a4831a --- /dev/null +++ b/cli/scripts/templates/src/__tests__/index.test.ts @@ -0,0 +1,7 @@ +import { sum } from "../."; + +describe("Hello", () => { + it("renders without crashing", () => { + expect(sum(1, 2)).toEqual(3); + }); +}); diff --git a/cli/scripts/templates/src/__tests__/tsconfig.json b/cli/scripts/templates/src/__tests__/tsconfig.json new file mode 100644 index 0000000..bf65be6 --- /dev/null +++ b/cli/scripts/templates/src/__tests__/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.json", + "include": [".", "../."] +} diff --git a/cli/scripts/templates/src/index.ts b/cli/scripts/templates/src/index.ts new file mode 100644 index 0000000..715e93e --- /dev/null +++ b/cli/scripts/templates/src/index.ts @@ -0,0 +1,3 @@ +export const sum = (a: number, b: number) => { + return a + b; +}; diff --git a/cli/scripts/templates/tsconfig.json b/cli/scripts/templates/tsconfig.json new file mode 100644 index 0000000..76ae392 --- /dev/null +++ b/cli/scripts/templates/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "dist" + }, + "include": ["src"], + "exclude": ["node_modules"] +} |
