Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffolded create-v2-addon-repo #1

Merged
merged 6 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-poets-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-v2-addon-repo": minor
---

Scaffolded create-v2-addon-repo
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
"author": "Isaac J. Lee",
"type": "module",
"scripts": {
"build": "pnpm --filter './packages/**' build",
"lint": "pnpm --filter '*' lint",
"lint:fix": "pnpm --filter '*' lint:fix",
"prepare": "pnpm build",
"release:changelog": "changeset version",
"release:publish": "changeset publish",
"release:publish": "pnpm build && changeset publish",
"test": "pnpm --filter '*' test"
},
"devDependencies": {
"@changesets/cli": "^2.27.7",
"@changesets/get-github-info": "^0.6.0",
"concurrently": "^8.2.2"
"@changesets/get-github-info": "^0.6.0"
},
"engines": {
"node": "18.* || >= 20",
Expand Down
13 changes: 13 additions & 0 deletions packages/create-v2-addon-repo/.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/create-v2-addon-repo/.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/create-v2-addon-repo/.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/create-v2-addon-repo/.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/create-v2-addon-repo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# create-v2-addon-repo
9 changes: 9 additions & 0 deletions packages/create-v2-addon-repo/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/create-v2-addon-repo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# create-v2-addon-repo
31 changes: 31 additions & 0 deletions packages/create-v2-addon-repo/bin/create-v2-addon-repo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node
'use strict';

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

import { runCodemod } from '../src/index.js';
import type { CodemodOptions } from '../src/types/index.js';

// Provide a title to the process in `ps`
process.title = 'create-v2-addon-repo';

// Set codemod options
const argv = yargs(hideBin(process.argv))
.option('name', {
demandOption: true,
describe: 'Name of your project',
type: 'string',
})
.option('root', {
describe: 'Where to run the codemod',
type: 'string',
})
.parseSync();

const codemodOptions: CodemodOptions = {
name: argv['name'],
projectRoot: argv['root'] ?? process.cwd(),
};

runCodemod(codemodOptions);
40 changes: 40 additions & 0 deletions packages/create-v2-addon-repo/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env sh

COMMAND="create-v2-addon-repo"
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/create-v2-addon-repo/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/create-v2-addon-repo.js $NAMED_ARGUMENTS --root="tests/fixtures/$FIXTURE/output"

echo "SUCCESS: Updated the output of $FIXTURE.\n"
20 changes: 20 additions & 0 deletions packages/create-v2-addon-repo/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 "--name my-repo" \
typescript
56 changes: 56 additions & 0 deletions packages/create-v2-addon-repo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "create-v2-addon-repo",
"version": "0.0.0",
"private": true,
"description": "Create a repo with 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/create-v2-addon-repo.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.
8 changes: 8 additions & 0 deletions packages/create-v2-addon-repo/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createFilesFromBlueprints, createOptions } from './steps/index.js';
import type { CodemodOptions } from './types/index.js';

export function runCodemod(codemodOptions: CodemodOptions): void {
const options = createOptions(codemodOptions);

createFilesFromBlueprints(options);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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/index.js';
import { blueprintsRoot } from '../utils/blueprints.js';

function resolveBlueprintFilePath(blueprintFilePath: string): string {
return blueprintFilePath;
}

export function createFilesFromBlueprints(options: Options): void {
const { project } = options;

const blueprintFilePaths = findFiles('**/*', {
projectRoot: blueprintsRoot,
});

const fileMap = new Map(
blueprintFilePaths.map((blueprintFilePath) => {
const filePath = join(
project.name,
resolveBlueprintFilePath(blueprintFilePath),
);

const blueprintFile = readFileSync(
join(blueprintsRoot, blueprintFilePath),
'utf8',
);

const file = processTemplate(blueprintFile, {
options,
});

return [filePath, file];
}),
);

createFiles(fileMap, options);
}
12 changes: 12 additions & 0 deletions packages/create-v2-addon-repo/src/steps/create-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { CodemodOptions, Options } from '../types/index.js';

export function createOptions(codemodOptions: CodemodOptions): Options {
const { name, projectRoot } = codemodOptions;

return {
project: {
name,
},
projectRoot,
};
}
2 changes: 2 additions & 0 deletions packages/create-v2-addon-repo/src/steps/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';
13 changes: 13 additions & 0 deletions packages/create-v2-addon-repo/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type CodemodOptions = {
name: string;
projectRoot: string;
};

type Options = {
project: {
name: string;
};
projectRoot: string;
};

export type { CodemodOptions, Options };
1 change: 1 addition & 0 deletions packages/create-v2-addon-repo/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('typescript/input');
const outputProject = convertFixtureToJson('typescript/output');

export { inputProject, outputProject };
Empty file.
Empty file.
Loading