diff --git a/.changeset/dry-bananas-sing.md b/.changeset/dry-bananas-sing.md new file mode 100644 index 0000000..9466612 --- /dev/null +++ b/.changeset/dry-bananas-sing.md @@ -0,0 +1,5 @@ +--- +"blueprints-addon": minor +--- + +Scaffolded blueprints-addon diff --git a/packages/blueprints-addon/.eslintignore b/packages/blueprints-addon/.eslintignore new file mode 100644 index 0000000..771057c --- /dev/null +++ b/packages/blueprints-addon/.eslintignore @@ -0,0 +1,13 @@ +# compiled output +/dist/ +/dist-for-testing/ +/tmp/ + +# dependencies +/node_modules/ + +# misc +!.* +.*/ +/src/blueprints/ +/tests/fixtures/ diff --git a/packages/blueprints-addon/.eslintrc.cjs b/packages/blueprints-addon/.eslintrc.cjs new file mode 100644 index 0000000..c4e7794 --- /dev/null +++ b/packages/blueprints-addon/.eslintrc.cjs @@ -0,0 +1,15 @@ +'use strict'; + +require('@shared-configs/eslint-config-node/patch'); + +module.exports = { + extends: ['@shared-configs/eslint-config-node/typescript'], + overrides: [ + { + files: ['bin/**/*.{js,ts}'], + rules: { + 'n/hashbang': 'off', + }, + }, + ], +}; diff --git a/packages/blueprints-addon/.npmignore b/packages/blueprints-addon/.npmignore new file mode 100644 index 0000000..b0b3cfa --- /dev/null +++ b/packages/blueprints-addon/.npmignore @@ -0,0 +1,24 @@ +# compiled output +/dist-for-testing/ +/tmp/ + +# dependencies +/node_modules/ + +# misc +/.DS_Store +/.env* +/.eslintcache +/.eslintignore +/.eslintrc.cjs +/.git/ +/.github/ +/.gitignore +/.pnpm-debug.log +/.prettierignore +/.prettierrc.cjs +/build.sh +/codemod-test-fixture.sh +/codemod-test-fixtures.sh +/create-test-fixture.sh +/tests/ diff --git a/packages/blueprints-addon/.prettierrc.cjs b/packages/blueprints-addon/.prettierrc.cjs new file mode 100644 index 0000000..038d697 --- /dev/null +++ b/packages/blueprints-addon/.prettierrc.cjs @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('@shared-configs/prettier'); diff --git a/packages/blueprints-addon/CHANGELOG.md b/packages/blueprints-addon/CHANGELOG.md new file mode 100644 index 0000000..cd34df3 --- /dev/null +++ b/packages/blueprints-addon/CHANGELOG.md @@ -0,0 +1 @@ +# blueprints-addon diff --git a/packages/blueprints-addon/LICENSE.md b/packages/blueprints-addon/LICENSE.md new file mode 100644 index 0000000..cba618f --- /dev/null +++ b/packages/blueprints-addon/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2024 Isaac J. Lee + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/blueprints-addon/README.md b/packages/blueprints-addon/README.md new file mode 100644 index 0000000..cd34df3 --- /dev/null +++ b/packages/blueprints-addon/README.md @@ -0,0 +1 @@ +# blueprints-addon diff --git a/packages/blueprints-addon/bin/blueprints-addon.ts b/packages/blueprints-addon/bin/blueprints-addon.ts new file mode 100755 index 0000000..d6117ec --- /dev/null +++ b/packages/blueprints-addon/bin/blueprints-addon.ts @@ -0,0 +1,45 @@ +#!/usr/bin/env node +'use strict'; + +import yargs from 'yargs'; +import { hideBin } from 'yargs/helpers'; + +import { runNew } from '../src/index.js'; + +// Provide a title to the process in `ps` +process.title = 'blueprints-addon'; + +// Set codemod options +yargs(hideBin(process.argv)) + .command({ + builder: (yargs) => { + return yargs + .option('location', { + demandOption: true, + describe: "Location of the addon (e.g. 'ui/button')", + type: 'string', + }) + .option('name', { + demandOption: true, + describe: "Name of the addon (e.g. '@my-org-ui/button')", + type: 'string', + }) + .option('root', { + describe: 'Where to run the codemod', + type: 'string', + }); + }, + command: 'new', + describe: 'Create a v2 addon', + handler: (argv) => { + const codemodOptions = { + location: argv['location'], + name: argv['name'], + projectRoot: argv['root'] ?? process.cwd(), + }; + + runNew(codemodOptions); + }, + }) + .demandCommand() + .parseSync(); diff --git a/packages/blueprints-addon/build.sh b/packages/blueprints-addon/build.sh new file mode 100755 index 0000000..90a45e6 --- /dev/null +++ b/packages/blueprints-addon/build.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env sh + +COMMAND="blueprints-addon" +ENVIRONMENT=$1 + +if [ $ENVIRONMENT = "--production" ] +then + # Clean slate + rm -rf "dist" + + # Compile TypeScript + tsc --project "tsconfig.build.json" + + # Configure files + chmod +x "dist/bin/$COMMAND.js" + + if [ -d "src/blueprints" ] + then + cp -r "src/blueprints" "dist/src/blueprints" + fi + + echo "SUCCESS: Built dist.\n" + +elif [ $ENVIRONMENT = "--test" ] +then + # Clean slate + rm -rf "dist-for-testing" + + # Compile TypeScript + tsc --project "tsconfig.json" + + # Configure files + if [ -d "src/blueprints" ] + then + cp -r "src/blueprints" "dist-for-testing/src/blueprints" + fi + + echo "SUCCESS: Built dist-for-testing.\n" + +fi diff --git a/packages/blueprints-addon/codemod-test-fixture.sh b/packages/blueprints-addon/codemod-test-fixture.sh new file mode 100755 index 0000000..fd23fa5 --- /dev/null +++ b/packages/blueprints-addon/codemod-test-fixture.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env sh + +#---------- +# +# A. Purpose +# +# Fix the expected output of a test fixture after updating the source code. +# +# B. Usage +# +# For named arguments, do not include `=` between the flag and the value. +# Positional arguments are to appear at the end. +# +# ./codemod-test-fixture.sh [OPTIONAL-FLAGS] +# +#--------- + +# Get named arguments for the binary +while getopts ":N:" flag +do + case $flag in + N) NAMED_ARGUMENTS=$OPTARG;; + esac +done + +# Get fixture name +FIXTURE=${@:$OPTIND:1} + +if [ ! $FIXTURE ] +then + echo "ERROR: Please specify the fixture name.\n" + exit 1 +elif [ ! -d "tests/fixtures/$FIXTURE/input" ] +then + echo "ERROR: Input folder \`tests/fixtures/$FIXTURE/input\` does not exist.\n" + exit 1 +fi + +rm -r "tests/fixtures/$FIXTURE/output" +cp -r "tests/fixtures/$FIXTURE/input" "tests/fixtures/$FIXTURE/output" + +./dist/bin/blueprints-addon.js $NAMED_ARGUMENTS --root="tests/fixtures/$FIXTURE/output" + +echo "SUCCESS: Updated the output of $FIXTURE.\n" diff --git a/packages/blueprints-addon/codemod-test-fixtures.sh b/packages/blueprints-addon/codemod-test-fixtures.sh new file mode 100755 index 0000000..d47c0e3 --- /dev/null +++ b/packages/blueprints-addon/codemod-test-fixtures.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env sh + +#---------- +# +# A. Purpose +# +# Fix all test fixtures after updating the source code. +# +# B. Usage +# +# ./codemod-test-fixtures.sh +# +#--------- + +# Compile TypeScript +pnpm build + +./codemod-test-fixture.sh \ + -N "new --location ui/button --name @my-org-ui/button" \ + run-new/typescript diff --git a/packages/blueprints-addon/package.json b/packages/blueprints-addon/package.json new file mode 100644 index 0000000..a1f3ea9 --- /dev/null +++ b/packages/blueprints-addon/package.json @@ -0,0 +1,56 @@ +{ + "name": "blueprints-addon", + "version": "0.0.0", + "private": true, + "description": "Blueprints for v2 addons", + "keywords": [ + "codemod", + "ember-codemod", + "emberjs" + ], + "repository": { + "type": "git", + "url": "https://github.com/ijlee2/embroider-toolbox.git" + }, + "license": "MIT", + "author": "Isaac J. Lee", + "type": "module", + "main": "dist/src/index.js", + "bin": "dist/bin/blueprints-addon.js", + "directories": { + "test": "tests" + }, + "files": [ + "dist" + ], + "scripts": { + "build": "./build.sh --production", + "lint": "concurrently 'pnpm:lint:*(!fix)' --names 'lint:'", + "lint:fix": "concurrently 'pnpm:lint:*:fix' --names 'fix:'", + "lint:js": "eslint . --cache", + "lint:js:fix": "eslint . --fix", + "lint:types": "tsc --noEmit", + "test": "./build.sh --test && mt dist-for-testing --quiet" + }, + "dependencies": { + "@codemod-utils/blueprints": "^1.1.5", + "@codemod-utils/files": "^2.0.4", + "yargs": "^17.7.2" + }, + "devDependencies": { + "@codemod-utils/tests": "^1.1.7", + "@shared-configs/eslint-config-node": "workspace:*", + "@shared-configs/prettier": "workspace:*", + "@shared-configs/typescript": "workspace:*", + "@sondr3/minitest": "^0.1.2", + "@types/node": "^18.19.44", + "@types/yargs": "^17.0.33", + "concurrently": "^8.2.2", + "eslint": "^8.57.0", + "prettier": "^3.3.3", + "typescript": "^5.5.4" + }, + "engines": { + "node": "18.* || >= 20" + } +} diff --git a/packages/blueprints-addon/src/blueprints/run-new/.gitkeep b/packages/blueprints-addon/src/blueprints/run-new/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/packages/blueprints-addon/src/index.ts b/packages/blueprints-addon/src/index.ts new file mode 100644 index 0000000..473422d --- /dev/null +++ b/packages/blueprints-addon/src/index.ts @@ -0,0 +1 @@ +export * from './run-new.js'; diff --git a/packages/blueprints-addon/src/run-new.ts b/packages/blueprints-addon/src/run-new.ts new file mode 100644 index 0000000..96e5c33 --- /dev/null +++ b/packages/blueprints-addon/src/run-new.ts @@ -0,0 +1,11 @@ +import { + createFilesFromBlueprints, + createOptions, +} from './steps/run-new/index.js'; +import type { CodemodOptions } from './types/run-new.js'; + +export function runNew(codemodOptions: CodemodOptions): void { + const options = createOptions(codemodOptions); + + createFilesFromBlueprints(options); +} diff --git a/packages/blueprints-addon/src/steps/run-new/create-files-from-blueprints.ts b/packages/blueprints-addon/src/steps/run-new/create-files-from-blueprints.ts new file mode 100644 index 0000000..9584aee --- /dev/null +++ b/packages/blueprints-addon/src/steps/run-new/create-files-from-blueprints.ts @@ -0,0 +1,36 @@ +import { readFileSync } from 'node:fs'; +import { join } from 'node:path'; + +import { processTemplate } from '@codemod-utils/blueprints'; +import { createFiles, findFiles } from '@codemod-utils/files'; + +import type { Options } from '../../types/run-new.js'; +import { blueprintsRoot } from '../../utils/blueprints.js'; + +function resolveBlueprintFilePath(blueprintFilePath: string): string { + return blueprintFilePath; +} + +export function createFilesFromBlueprints(options: Options): void { + const cwd = join(blueprintsRoot, 'run-new'); + + const blueprintFilePaths = findFiles('**/*', { + projectRoot: cwd, + }); + + const fileMap = new Map( + blueprintFilePaths.map((blueprintFilePath) => { + const filePath = resolveBlueprintFilePath(blueprintFilePath); + + const blueprintFile = readFileSync(join(cwd, blueprintFilePath), 'utf8'); + + const file = processTemplate(blueprintFile, { + options, + }); + + return [filePath, file]; + }), + ); + + createFiles(fileMap, options); +} diff --git a/packages/blueprints-addon/src/steps/run-new/create-options.ts b/packages/blueprints-addon/src/steps/run-new/create-options.ts new file mode 100644 index 0000000..ed19ab0 --- /dev/null +++ b/packages/blueprints-addon/src/steps/run-new/create-options.ts @@ -0,0 +1,15 @@ +import { join } from 'node:path'; + +import type { CodemodOptions, Options } from '../../types/run-new.js'; + +export function createOptions(codemodOptions: CodemodOptions): Options { + const { location, name, projectRoot } = codemodOptions; + + return { + addon: { + location: join('packages', location), + name, + }, + projectRoot, + }; +} diff --git a/packages/blueprints-addon/src/steps/run-new/index.ts b/packages/blueprints-addon/src/steps/run-new/index.ts new file mode 100644 index 0000000..0cb0adb --- /dev/null +++ b/packages/blueprints-addon/src/steps/run-new/index.ts @@ -0,0 +1,2 @@ +export * from './create-files-from-blueprints.js'; +export * from './create-options.js'; diff --git a/packages/blueprints-addon/src/types/run-new.ts b/packages/blueprints-addon/src/types/run-new.ts new file mode 100644 index 0000000..a05bd8e --- /dev/null +++ b/packages/blueprints-addon/src/types/run-new.ts @@ -0,0 +1,15 @@ +type CodemodOptions = { + location: string; + name: string; + projectRoot: string; +}; + +type Options = { + addon: { + location: string; + name: string; + }; + projectRoot: string; +}; + +export type { CodemodOptions, Options }; diff --git a/packages/blueprints-addon/src/utils/blueprints.ts b/packages/blueprints-addon/src/utils/blueprints.ts new file mode 100644 index 0000000..1e54087 --- /dev/null +++ b/packages/blueprints-addon/src/utils/blueprints.ts @@ -0,0 +1 @@ +export * from './blueprints/blueprints-root.js'; diff --git a/packages/blueprints-addon/src/utils/blueprints/blueprints-root.ts b/packages/blueprints-addon/src/utils/blueprints/blueprints-root.ts new file mode 100644 index 0000000..237d2fb --- /dev/null +++ b/packages/blueprints-addon/src/utils/blueprints/blueprints-root.ts @@ -0,0 +1,7 @@ +import { join } from 'node:path'; + +import { getFilePath } from '@codemod-utils/blueprints'; + +const fileURL = import.meta.url; + +export const blueprintsRoot = join(getFilePath(fileURL), '../../blueprints'); diff --git a/packages/blueprints-addon/tests/fixtures/run-new/typescript/index.ts b/packages/blueprints-addon/tests/fixtures/run-new/typescript/index.ts new file mode 100644 index 0000000..eb9b40b --- /dev/null +++ b/packages/blueprints-addon/tests/fixtures/run-new/typescript/index.ts @@ -0,0 +1,6 @@ +import { convertFixtureToJson } from '@codemod-utils/tests'; + +const inputProject = convertFixtureToJson('run-new/typescript/input'); +const outputProject = convertFixtureToJson('run-new/typescript/output'); + +export { inputProject, outputProject }; diff --git a/packages/blueprints-addon/tests/fixtures/run-new/typescript/input/.gitkeep b/packages/blueprints-addon/tests/fixtures/run-new/typescript/input/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/packages/blueprints-addon/tests/fixtures/run-new/typescript/output/.gitkeep b/packages/blueprints-addon/tests/fixtures/run-new/typescript/output/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/packages/blueprints-addon/tests/helpers/shared-test-setups/run-new/typescript.ts b/packages/blueprints-addon/tests/helpers/shared-test-setups/run-new/typescript.ts new file mode 100644 index 0000000..381bb3c --- /dev/null +++ b/packages/blueprints-addon/tests/helpers/shared-test-setups/run-new/typescript.ts @@ -0,0 +1,17 @@ +import type { CodemodOptions, Options } from '../../../../src/types/run-new.js'; + +const codemodOptions: CodemodOptions = { + location: 'ui/button', + name: '@my-org-ui/button', + projectRoot: 'tmp/run-new/typescript', +}; + +const options: Options = { + addon: { + location: 'packages/ui/button', + name: '@my-org-ui/button', + }, + projectRoot: 'tmp/run-new/typescript', +}; + +export { codemodOptions, options }; diff --git a/packages/blueprints-addon/tests/index/run-new/typescript.test.ts b/packages/blueprints-addon/tests/index/run-new/typescript.test.ts new file mode 100644 index 0000000..e1306d8 --- /dev/null +++ b/packages/blueprints-addon/tests/index/run-new/typescript.test.ts @@ -0,0 +1,21 @@ +import { assertFixture, loadFixture, test } from '@codemod-utils/tests'; + +import { runNew } from '../../../src/index.js'; +import { + inputProject, + outputProject, +} from '../../fixtures/run-new/typescript/index.js'; +import { codemodOptions } from '../../helpers/shared-test-setups/run-new/typescript.js'; + +test('index | run-new > typescript', function () { + loadFixture(inputProject, codemodOptions); + + runNew(codemodOptions); + + assertFixture(outputProject, codemodOptions); + + // Check idempotence + runNew(codemodOptions); + + assertFixture(outputProject, codemodOptions); +}); diff --git a/packages/blueprints-addon/tests/utils/blueprints/blueprints-root.test.ts b/packages/blueprints-addon/tests/utils/blueprints/blueprints-root.test.ts new file mode 100644 index 0000000..df5ed4f --- /dev/null +++ b/packages/blueprints-addon/tests/utils/blueprints/blueprints-root.test.ts @@ -0,0 +1,7 @@ +import { assert, test } from '@codemod-utils/tests'; + +import { blueprintsRoot } from '../../../src/utils/blueprints.js'; + +test('utils | blueprints | blueprints-root', function () { + assert.strictEqual(blueprintsRoot.endsWith('src/blueprints'), true); +}); diff --git a/packages/blueprints-addon/tsconfig.build.json b/packages/blueprints-addon/tsconfig.build.json new file mode 100644 index 0000000..d65c669 --- /dev/null +++ b/packages/blueprints-addon/tsconfig.build.json @@ -0,0 +1,9 @@ +{ + "extends": "@shared-configs/typescript/node18", + "compilerOptions": { + "declaration": false, + "outDir": "dist" + }, + "include": ["bin", "src"], + "exclude": ["src/blueprints"] +} diff --git a/packages/blueprints-addon/tsconfig.json b/packages/blueprints-addon/tsconfig.json new file mode 100644 index 0000000..498b990 --- /dev/null +++ b/packages/blueprints-addon/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@shared-configs/typescript/node18", + "compilerOptions": { + "declaration": false, + "outDir": "dist-for-testing" + }, + "include": ["bin", "src", "tests"], + "exclude": ["src/blueprints", "tests/fixtures"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5268643..91fc438 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -221,6 +221,52 @@ importers: specifier: ^3.3.3 version: 3.3.3 + packages/blueprints-addon: + dependencies: + '@codemod-utils/blueprints': + specifier: ^1.1.5 + version: 1.1.5 + '@codemod-utils/files': + specifier: ^2.0.4 + version: 2.0.4 + yargs: + specifier: ^17.7.2 + version: 17.7.2 + devDependencies: + '@codemod-utils/tests': + specifier: ^1.1.7 + version: 1.1.7(@sondr3/minitest@0.1.2) + '@shared-configs/eslint-config-node': + specifier: workspace:* + version: link:../../configs/eslint/node + '@shared-configs/prettier': + specifier: workspace:* + version: link:../../configs/prettier + '@shared-configs/typescript': + specifier: workspace:* + version: link:../../configs/typescript + '@sondr3/minitest': + specifier: ^0.1.2 + version: 0.1.2 + '@types/node': + specifier: ^18.19.44 + version: 18.19.44 + '@types/yargs': + specifier: ^17.0.33 + version: 17.0.33 + concurrently: + specifier: ^8.2.2 + version: 8.2.2 + eslint: + specifier: ^8.57.0 + version: 8.57.0 + prettier: + specifier: ^3.3.3 + version: 3.3.3 + typescript: + specifier: ^5.5.4 + version: 5.5.4 + packages/create-v2-addon-repo: dependencies: '@codemod-utils/blueprints': diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 9fdb336..f109de0 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,4 @@ packages: - configs/** + - packages/blueprints-addon - packages/create-v2-addon-repo