Skip to content

Commit 986956d

Browse files
authored
Refactored utilities (#30)
* chore: Updated CI workflow * chore: Sorted file paths (an order is not guaranteed in glob@v9) * refactor: Extracted function * refactor: Renamed variables (copyFiles) * refactor: Renamed variables (createFiles) * refactor: Renamed variables (mapFilePaths) * refactor: Renamed variables (moveFiles) * refactor: Updated API (removeDirectoryIfEmpty) * refactor: Renamed variables (removeFiles) * chore: Installed @codemod-utils packages * refactor: Simplified utilities for blueprints * chore: Removed utilities for files * refactor: Simplified utilities for JSON * chore: Removed utilities for tests * chore: Removed unused dependencies * chore: Ran sort-package-json --------- Co-authored-by: ijlee2 <[email protected]>
1 parent f389ecd commit 986956d

File tree

134 files changed

+463
-1421
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+463
-1421
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ env:
1111

1212
jobs:
1313
lint:
14-
name: Lint files
14+
name: Lint
1515
runs-on: ubuntu-latest
1616
timeout-minutes: 5
1717
steps:
@@ -31,8 +31,8 @@ jobs:
3131
run: yarn lint
3232

3333

34-
test-app:
35-
name: Test app
34+
test:
35+
name: Test
3636
runs-on: ubuntu-latest
3737
timeout-minutes: 5
3838
steps:

create-test-fixture.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fi
5151
mkdir -p "tests/fixtures/$DESTINATION/input"
5252
mkdir -p "tests/fixtures/$DESTINATION/output"
5353

54-
echo "import { convertFixtureToJson } from '../../helpers/testing.js';
54+
echo "import { convertFixtureToJson } from '@codemod-utils/tests';
5555
5656
const inputProject = convertFixtureToJson('$DESTINATION/input');
5757
const outputProject = convertFixtureToJson('$DESTINATION/output');

package.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,54 @@
1717
},
1818
"license": "MIT",
1919
"author": "Isaac J. Lee",
20-
"main": "src/index.js",
2120
"type": "module",
21+
"main": "src/index.js",
2222
"bin": "./bin/ember-codemod-v1-to-v2.js",
23+
"directories": {
24+
"test": "tests"
25+
},
2326
"files": [
2427
"bin/",
2528
"src/"
2629
],
27-
"directories": {
28-
"test": "tests"
29-
},
3030
"scripts": {
3131
"changelog": "lerna-changelog",
3232
"lint": "eslint . --cache",
3333
"lint:fix": "eslint . --fix",
3434
"test": "mt tests --quiet"
3535
},
36-
"changelog": {
37-
"labels": {
38-
"breaking": "Breaking Change",
39-
"bug": "Bug Fix",
40-
"enhance: code": "Enhancement",
41-
"enhance: dependency": "Internal",
42-
"enhance: documentation": "Documentation",
43-
"user feedback": "User Feedback"
44-
}
45-
},
4636
"dependencies": {
47-
"glob": "^10.2.2",
48-
"lodash.template": "^4.5.0",
37+
"@codemod-utils/blueprints": "^0.1.1",
38+
"@codemod-utils/files": "^0.1.0",
39+
"@codemod-utils/json": "^0.1.1",
4940
"strip-json-comments": "^5.0.0",
5041
"yargs": "^17.7.2"
5142
},
5243
"devDependencies": {
5344
"@babel/core": "^7.21.8",
5445
"@babel/eslint-parser": "^7.21.8",
46+
"@codemod-utils/tests": "^0.1.0",
5547
"@sondr3/minitest": "^0.1.1",
5648
"eslint": "^8.39.0",
5749
"eslint-config-prettier": "^8.8.0",
5850
"eslint-plugin-import": "^2.27.5",
5951
"eslint-plugin-n": "^15.7.0",
6052
"eslint-plugin-prettier": "^4.2.1",
6153
"eslint-plugin-simple-import-sort": "^10.0.0",
62-
"fixturify": "^3.0.0",
6354
"lerna-changelog": "^2.2.0",
6455
"prettier": "^2.8.8"
6556
},
6657
"engines": {
6758
"node": ">= 16"
59+
},
60+
"changelog": {
61+
"labels": {
62+
"breaking": "Breaking Change",
63+
"bug": "Bug Fix",
64+
"enhance: code": "Enhancement",
65+
"enhance: dependency": "Internal",
66+
"enhance: documentation": "Documentation",
67+
"user feedback": "User Feedback"
68+
}
6869
}
6970
}

src/migration/ember-addon/steps/analyze-addon.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { decideVersion } from '../../../utils/blueprints.js';
2-
import { findFiles, renameDirectory } from '../../../utils/files.js';
1+
import { findFiles, renameDirectory } from '@codemod-utils/files';
2+
3+
import { getVersion } from '../../../utils/blueprints.js';
34

45
function getAppReexports(options) {
56
const { projectRoot } = options;
@@ -20,7 +21,7 @@ function getAppReexports(options) {
2021

2122
function getProjectRootDevDependencies(options) {
2223
return {
23-
concurrently: decideVersion('concurrently', options),
24+
concurrently: getVersion('concurrently', options),
2425
};
2526
}
2627

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import { readFileSync } from 'node:fs';
22
import { join } from 'node:path';
33

4-
import { blueprintsRoot, processTemplate } from '../../../utils/blueprints.js';
5-
import { createFiles, findFiles } from '../../../utils/files.js';
4+
import { processTemplate } from '@codemod-utils/blueprints';
5+
import { createFiles, findFiles } from '@codemod-utils/files';
66

7-
function getFilePath(blueprintFilePath, options) {
8-
const { locations } = options;
9-
10-
return blueprintFilePath
11-
.replace('__addonLocation__', locations.addon)
12-
.replace('__gitignore__', '.gitignore')
13-
.replace('__testAppLocation__', locations.testApp);
14-
}
7+
import { blueprintsRoot } from '../../../utils/blueprints.js';
158

169
function getFilesToSkip(options) {
1710
const { packageManager, packages } = options;
@@ -30,23 +23,29 @@ function getFilesToSkip(options) {
3023
return [...files];
3124
}
3225

26+
function resolveBlueprintFilePath(blueprintFilePath, options) {
27+
const { locations } = options;
28+
29+
return blueprintFilePath
30+
.replace('__addonLocation__', locations.addon)
31+
.replace('__gitignore__', '.gitignore')
32+
.replace('__testAppLocation__', locations.testApp);
33+
}
34+
3335
export function createFilesFromBlueprint(context, options) {
3436
const filesToSkip = getFilesToSkip(options);
35-
const emberAddonBlueprintsRoot = join(blueprintsRoot, 'ember-addon');
37+
const cwd = join(blueprintsRoot, 'ember-addon');
3638

3739
const blueprintFilePaths = findFiles('**/*', {
38-
cwd: emberAddonBlueprintsRoot,
40+
cwd,
3941
ignoreList: filesToSkip,
4042
});
4143

42-
const fileMapping = new Map(
44+
const fileMap = new Map(
4345
blueprintFilePaths.map((blueprintFilePath) => {
44-
const filePath = getFilePath(blueprintFilePath, options);
46+
const filePath = resolveBlueprintFilePath(blueprintFilePath, options);
4547

46-
const blueprintFile = readFileSync(
47-
join(emberAddonBlueprintsRoot, blueprintFilePath),
48-
'utf8',
49-
);
48+
const blueprintFile = readFileSync(join(cwd, blueprintFilePath), 'utf8');
5049

5150
const file = processTemplate(blueprintFile, {
5251
context,
@@ -57,5 +56,5 @@ export function createFilesFromBlueprint(context, options) {
5756
}),
5857
);
5958

60-
createFiles(fileMapping, options);
59+
createFiles(fileMap, options);
6160
}

src/migration/ember-addon/steps/create-options.js

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,28 @@
1-
import { readFileSync } from 'node:fs';
2-
import { join } from 'node:path';
3-
4-
import { findFiles, unionize } from '../../../utils/files.js';
5-
6-
function validatePackageJson({ name, version }) {
7-
if (!name) {
8-
throw new SyntaxError('Package name is missing.');
9-
}
10-
11-
if (name.includes('/')) {
12-
// eslint-disable-next-line no-unused-vars
13-
const [_scope, packageName] = name.split('/');
14-
15-
if (!packageName) {
16-
throw new SyntaxError('Package name is missing.');
17-
}
18-
}
19-
20-
if (!version) {
21-
throw new SyntaxError('Package version is missing.');
22-
}
23-
}
1+
import { findFiles, unionize } from '@codemod-utils/files';
2+
import { readPackageJson } from '@codemod-utils/json';
243

254
function analyzePackageJson(codemodOptions) {
26-
const { projectRoot } = codemodOptions;
27-
28-
try {
29-
const packageJsonFile = readFileSync(
30-
join(projectRoot, 'package.json'),
31-
'utf8',
32-
);
33-
34-
const {
35-
dependencies,
36-
devDependencies,
37-
'ember-addon': emberAddon,
38-
name,
39-
version,
40-
} = JSON.parse(packageJsonFile);
41-
42-
validatePackageJson({ name, version });
43-
44-
const projectDependencies = new Map([
45-
...Object.entries(dependencies ?? {}),
46-
...Object.entries(devDependencies ?? {}),
47-
]);
5+
const {
6+
dependencies,
7+
devDependencies,
8+
'ember-addon': emberAddon,
9+
name,
10+
version,
11+
} = readPackageJson(codemodOptions);
12+
13+
const projectDependencies = new Map([
14+
...Object.entries(dependencies ?? {}),
15+
...Object.entries(devDependencies ?? {}),
16+
]);
4817

49-
return {
50-
dependencies: projectDependencies,
51-
hasGlint: projectDependencies.has('@glint/core'),
52-
hasTypeScript: projectDependencies.has('typescript'),
53-
isV1Addon: Boolean(emberAddon),
54-
name,
55-
version,
56-
};
57-
} catch (e) {
58-
throw new SyntaxError(
59-
`ERROR: package.json is missing or is not valid. (${e.message})\n`,
60-
);
61-
}
18+
return {
19+
dependencies: projectDependencies,
20+
hasGlint: projectDependencies.has('@glint/core'),
21+
hasTypeScript: projectDependencies.has('typescript'),
22+
isV1Addon: Boolean(emberAddon),
23+
name,
24+
version,
25+
};
6226
}
6327

6428
function analyzePackageManager(codemodOptions) {

src/migration/ember-addon/steps/move-addon-files.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
mapFilePaths,
44
moveFiles,
55
removeFiles,
6-
} from '../../../utils/files.js';
6+
} from '@codemod-utils/files';
77

88
function moveAddonFolder(options) {
99
const { locations, projectRoot } = options;
@@ -12,12 +12,12 @@ function moveAddonFolder(options) {
1212
cwd: projectRoot,
1313
});
1414

15-
const pathMapping = mapFilePaths(filePaths, {
15+
const filePathMap = mapFilePaths(filePaths, {
1616
from: 'addon',
1717
to: `${locations.addon}/src`,
1818
});
1919

20-
moveFiles(pathMapping, options);
20+
moveFiles(filePathMap, options);
2121
}
2222

2323
function moveAddonTestSupportFolder(options) {
@@ -27,12 +27,12 @@ function moveAddonTestSupportFolder(options) {
2727
cwd: projectRoot,
2828
});
2929

30-
const pathMapping = mapFilePaths(filePaths, {
30+
const filePathMap = mapFilePaths(filePaths, {
3131
from: 'addon-test-support',
3232
to: `${locations.addon}/src/test-support`,
3333
});
3434

35-
moveFiles(pathMapping, options);
35+
moveFiles(filePathMap, options);
3636
}
3737

3838
function moveBlueprintsFolder(options) {
@@ -42,12 +42,12 @@ function moveBlueprintsFolder(options) {
4242
cwd: projectRoot,
4343
});
4444

45-
const pathMapping = mapFilePaths(filePaths, {
45+
const filePathMap = mapFilePaths(filePaths, {
4646
from: 'blueprints',
4747
to: `${locations.addon}/blueprints`,
4848
});
4949

50-
moveFiles(pathMapping, options);
50+
moveFiles(filePathMap, options);
5151
}
5252

5353
function movePublicFolder(options) {
@@ -57,12 +57,12 @@ function movePublicFolder(options) {
5757
cwd: projectRoot,
5858
});
5959

60-
const pathMapping = mapFilePaths(filePaths, {
60+
const filePathMap = mapFilePaths(filePaths, {
6161
from: 'public',
6262
to: `${locations.addon}/public`,
6363
});
6464

65-
moveFiles(pathMapping, options);
65+
moveFiles(filePathMap, options);
6666
}
6767

6868
function removeAppFolder(options) {

0 commit comments

Comments
 (0)