Skip to content

Commit

Permalink
Scaffolded blueprints-addon (#2)
Browse files Browse the repository at this point in the history
* chore: Scaffolded blueprints-addon

* chore: Added lockfile

* feature: Defined options for new command

* refactor: Reorganized files to allow more commands

* chore: Added changeset

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 authored Aug 14, 2024
1 parent eb44212 commit ce92bde
Show file tree
Hide file tree
Showing 32 changed files with 480 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-bananas-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"blueprints-addon": minor
---

Scaffolded blueprints-addon
13 changes: 13 additions & 0 deletions packages/blueprints-addon/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# compiled output
/dist/
/dist-for-testing/
/tmp/

# dependencies
/node_modules/

# misc
!.*
.*/
/src/blueprints/
/tests/fixtures/
15 changes: 15 additions & 0 deletions packages/blueprints-addon/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -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',
},
},
],
};
24 changes: 24 additions & 0 deletions packages/blueprints-addon/.npmignore
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions packages/blueprints-addon/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('@shared-configs/prettier');
1 change: 1 addition & 0 deletions packages/blueprints-addon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# blueprints-addon
9 changes: 9 additions & 0 deletions packages/blueprints-addon/LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions packages/blueprints-addon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# blueprints-addon
45 changes: 45 additions & 0 deletions packages/blueprints-addon/bin/blueprints-addon.ts
Original file line number Diff line number Diff line change
@@ -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();
40 changes: 40 additions & 0 deletions packages/blueprints-addon/build.sh
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions packages/blueprints-addon/codemod-test-fixture.sh
Original file line number Diff line number Diff line change
@@ -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] <FIXTURE-NAME>
#
#---------

# 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"
20 changes: 20 additions & 0 deletions packages/blueprints-addon/codemod-test-fixtures.sh
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions packages/blueprints-addon/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Empty file.
1 change: 1 addition & 0 deletions packages/blueprints-addon/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './run-new.js';
11 changes: 11 additions & 0 deletions packages/blueprints-addon/src/run-new.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 15 additions & 0 deletions packages/blueprints-addon/src/steps/run-new/create-options.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
2 changes: 2 additions & 0 deletions packages/blueprints-addon/src/steps/run-new/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './create-files-from-blueprints.js';
export * from './create-options.js';
15 changes: 15 additions & 0 deletions packages/blueprints-addon/src/types/run-new.ts
Original file line number Diff line number Diff line change
@@ -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 };
1 change: 1 addition & 0 deletions packages/blueprints-addon/src/utils/blueprints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './blueprints/blueprints-root.js';
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -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 };
Empty file.
Empty file.
Loading

0 comments on commit ce92bde

Please sign in to comment.