Skip to content

Commit 610d178

Browse files
authored
Wrapped globSync and extracted blueprintRoot (#24)
* refactor: Created a function that wraps globSync * refactor: Removed unused argument * refactor: Renamed variable * refactor: Extracted function (unionize) * refactor: Extracted constant (blueprintRoot) * chore: Added tests * refactor: Extracted function (validatePackageJson) * chore: Added warning --------- Co-authored-by: ijlee2 <[email protected]>
1 parent e2e0135 commit 610d178

File tree

16 files changed

+196
-122
lines changed

16 files changed

+196
-122
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { globSync } from 'glob';
2-
31
import { decideVersion } from '../../../utils/blueprints.js';
4-
import { renameDirectory } from '../../../utils/files.js';
2+
import { findFiles, renameDirectory } from '../../../utils/files.js';
53

64
function getAppReexports(options) {
75
const { projectRoot } = options;
86

9-
const filePaths = globSync('app/**/*.js', {
7+
const filePaths = findFiles('app/**/*.js', {
108
cwd: projectRoot,
119
});
1210

@@ -30,10 +28,8 @@ function getProjectRootDevDependencies(options) {
3028
function getPublicAssets(options) {
3129
const { projectRoot } = options;
3230

33-
const filePaths = globSync('public/**/*', {
31+
const filePaths = findFiles('public/**/*', {
3432
cwd: projectRoot,
35-
dot: true,
36-
nodir: true,
3733
});
3834

3935
return filePaths
@@ -49,7 +45,7 @@ function getPublicAssets(options) {
4945
function getPublicEntrypoints(options) {
5046
const { projectRoot } = options;
5147

52-
const filePaths = globSync('{addon,addon-test-support}/**/*.{js,ts}', {
48+
const filePaths = findFiles('{addon,addon-test-support}/**/*.{js,ts}', {
5349
cwd: projectRoot,
5450
});
5551

src/migration/ember-addon/steps/create-files-from-blueprint.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
import { readFileSync } from 'node:fs';
2-
import { dirname, join } from 'node:path';
3-
import { fileURLToPath } from 'node:url';
2+
import { join } from 'node:path';
43

5-
import { globSync } from 'glob';
6-
7-
import { processTemplate } from '../../../utils/blueprints.js';
8-
import { createFiles } from '../../../utils/files.js';
9-
10-
const __filename = fileURLToPath(import.meta.url);
11-
const __dirname = dirname(__filename);
12-
13-
function getBlueprintRoot() {
14-
const codemodRoot = join(__dirname, '../../../..');
15-
16-
return join(codemodRoot, 'src/blueprints/ember-addon');
17-
}
4+
import { blueprintRoot, processTemplate } from '../../../utils/blueprints.js';
5+
import { createFiles, findFiles } from '../../../utils/files.js';
186

197
function getFilePath(blueprintFilePath, options) {
208
const { locations } = options;
@@ -43,15 +31,11 @@ function getFilesToSkip(options) {
4331
}
4432

4533
export function createFilesFromBlueprint(context, options) {
46-
const blueprintRoot = getBlueprintRoot();
47-
4834
const filesToSkip = getFilesToSkip(options);
4935

50-
const blueprintFilePaths = globSync('**/*', {
36+
const blueprintFilePaths = findFiles('**/*', {
5137
cwd: blueprintRoot,
52-
dot: true,
53-
ignore: filesToSkip,
54-
nodir: true,
38+
ignoreList: filesToSkip,
5539
});
5640

5741
const fileMapping = new Map(

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

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
import { readFileSync } from 'node:fs';
22
import { join } from 'node:path';
33

4-
import { globSync } from 'glob';
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+
}
524

625
function analyzePackageJson(codemodOptions) {
726
const { projectRoot } = codemodOptions;
@@ -20,13 +39,7 @@ function analyzePackageJson(codemodOptions) {
2039
version,
2140
} = JSON.parse(packageJsonFile);
2241

23-
if (!name) {
24-
throw new SyntaxError('Package name is missing.');
25-
}
26-
27-
if (!version) {
28-
throw new SyntaxError('Package version is missing.');
29-
}
42+
validatePackageJson({ name, version });
3043

3144
const projectDependencies = new Map([
3245
...Object.entries(dependencies ?? {}),
@@ -51,11 +64,19 @@ function analyzePackageJson(codemodOptions) {
5164
function analyzePackageManager(codemodOptions) {
5265
const { projectRoot } = codemodOptions;
5366

54-
const lockFiles = globSync('{package-lock.json,pnpm-lock.yaml,yarn.lock}', {
67+
const mapping = new Map([
68+
['package-lock.json', 'npm'],
69+
['pnpm-lock.yaml', 'pnpm'],
70+
['yarn.lock', 'yarn'],
71+
]);
72+
73+
const lockFiles = [...mapping.keys()];
74+
75+
const filePaths = findFiles(unionize(lockFiles), {
5576
cwd: projectRoot,
5677
});
5778

58-
if (lockFiles.length !== 1) {
79+
if (filePaths.length !== 1) {
5980
console.warn('WARNING: Package manager is unknown. Yarn will be assumed.');
6081

6182
return {
@@ -65,12 +86,12 @@ function analyzePackageManager(codemodOptions) {
6586
};
6687
}
6788

68-
const [lockFile] = lockFiles;
89+
const packageManager = mapping.get(filePaths[0]);
6990

7091
return {
71-
isNpm: lockFile === 'package-lock.json',
72-
isPnpm: lockFile === 'pnpm-lock.yaml',
73-
isYarn: lockFile === 'yarn.lock',
92+
isNpm: packageManager === 'npm',
93+
isPnpm: packageManager === 'pnpm',
94+
isYarn: packageManager === 'yarn',
7495
};
7596
}
7697

@@ -81,13 +102,7 @@ function deriveAddonLocation(addonPackage) {
81102
}
82103

83104
// eslint-disable-next-line no-unused-vars
84-
const [scope, packageName] = addonPackage.name.split('/');
85-
86-
if (!packageName) {
87-
throw new SyntaxError(
88-
`ERROR: In package.json, the package name \`${addonPackage.name}\` is not valid.`
89-
);
90-
}
105+
const [_scope, packageName] = addonPackage.name.split('/');
91106

92107
return packageName;
93108
}

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { globSync } from 'glob';
2-
3-
import { mapFilePaths, moveFiles, removeFiles } from '../../../utils/files.js';
1+
import {
2+
findFiles,
3+
mapFilePaths,
4+
moveFiles,
5+
removeFiles,
6+
} from '../../../utils/files.js';
47

58
function moveAddonFolder(options) {
69
const { locations, projectRoot } = options;
710

8-
const filePaths = globSync('addon/**/*', {
11+
const filePaths = findFiles('addon/**/*', {
912
cwd: projectRoot,
10-
dot: true,
11-
nodir: true,
1213
});
1314

1415
const pathMapping = mapFilePaths(filePaths, {
@@ -22,10 +23,8 @@ function moveAddonFolder(options) {
2223
function moveAddonTestSupportFolder(options) {
2324
const { locations, projectRoot } = options;
2425

25-
const filePaths = globSync('addon-test-support/**/*', {
26+
const filePaths = findFiles('addon-test-support/**/*', {
2627
cwd: projectRoot,
27-
dot: true,
28-
nodir: true,
2928
});
3029

3130
const pathMapping = mapFilePaths(filePaths, {
@@ -39,10 +38,8 @@ function moveAddonTestSupportFolder(options) {
3938
function moveBlueprintsFolder(options) {
4039
const { locations, projectRoot } = options;
4140

42-
const filePaths = globSync('blueprints/**/*', {
41+
const filePaths = findFiles('blueprints/**/*', {
4342
cwd: projectRoot,
44-
dot: true,
45-
nodir: true,
4643
});
4744

4845
const pathMapping = mapFilePaths(filePaths, {
@@ -56,10 +53,8 @@ function moveBlueprintsFolder(options) {
5653
function movePublicFolder(options) {
5754
const { locations, projectRoot } = options;
5855

59-
const filePaths = globSync('public/**/*', {
56+
const filePaths = findFiles('public/**/*', {
6057
cwd: projectRoot,
61-
dot: true,
62-
nodir: true,
6358
});
6459

6560
const pathMapping = mapFilePaths(filePaths, {
@@ -73,10 +68,8 @@ function movePublicFolder(options) {
7368
function removeAppFolder(options) {
7469
const { projectRoot } = options;
7570

76-
const filePaths = globSync('app/**/*', {
71+
const filePaths = findFiles('app/**/*', {
7772
cwd: projectRoot,
78-
dot: true,
79-
nodir: true,
8073
});
8174

8275
removeFiles(filePaths, options);

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
import { globSync } from 'glob';
2-
31
import {
42
copyFiles,
3+
findFiles,
54
mapFilePaths,
65
moveFiles,
76
removeFiles,
7+
unionize,
88
} from '../../../utils/files.js';
99

10-
function globPattern(files) {
11-
if (files.length <= 1) {
12-
return files.join(',');
13-
}
14-
15-
return `{${files.join(',')}}`;
16-
}
17-
1810
function copyToAddon(options) {
1911
const { locations, projectRoot } = options;
2012

2113
const files = ['LICENSE.md', 'README.md'];
2214

23-
const filePaths = globSync(globPattern(files), {
15+
const filePaths = findFiles(unionize(files), {
2416
cwd: projectRoot,
2517
});
2618

@@ -55,7 +47,7 @@ function moveToAddonAndTestApp(options) {
5547
files.add('tsconfig.json');
5648
}
5749

58-
const filePaths = globSync(globPattern([...files]), {
50+
const filePaths = findFiles(unionize([...files]), {
5951
cwd: projectRoot,
6052
});
6153

@@ -86,7 +78,7 @@ function moveToTestApp(options) {
8678
'testem.js',
8779
];
8880

89-
const filePaths = globSync(globPattern(files), {
81+
const filePaths = findFiles(unionize(files), {
9082
cwd: projectRoot,
9183
});
9284

@@ -103,7 +95,7 @@ function removeFromProjectRoot(options) {
10395

10496
const files = ['.npmignore', 'index.js'];
10597

106-
const filePaths = globSync(globPattern(files), {
98+
const filePaths = findFiles(unionize(files), {
10799
cwd: projectRoot,
108100
});
109101

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

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

4-
import { globSync } from 'glob';
5-
6-
import { mapFilePaths, moveFiles } from '../../../utils/files.js';
4+
import { findFiles, mapFilePaths, moveFiles } from '../../../utils/files.js';
75

86
function moveTestsFolder(options) {
97
const { locations, projectRoot } = options;
108

11-
let filePaths = globSync('tests/dummy/**/*', {
9+
let filePaths = findFiles('tests/dummy/**/*', {
1210
cwd: projectRoot,
13-
dot: true,
14-
nodir: true,
1511
});
1612

1713
let pathMapping = mapFilePaths(filePaths, {
@@ -21,11 +17,9 @@ function moveTestsFolder(options) {
2117

2218
moveFiles(pathMapping, options);
2319

24-
filePaths = globSync('tests/**/*', {
20+
filePaths = findFiles('tests/**/*', {
2521
cwd: projectRoot,
26-
dot: true,
27-
ignore: 'tests/dummy/**/*',
28-
nodir: true,
22+
ignoreList: ['tests/dummy/**/*'],
2923
});
3024

3125
pathMapping = mapFilePaths(filePaths, {
@@ -43,10 +37,8 @@ function moveTypesFolder(options) {
4337
return;
4438
}
4539

46-
let filePaths = globSync('types/dummy/**/*', {
40+
let filePaths = findFiles('types/dummy/**/*', {
4741
cwd: projectRoot,
48-
dot: true,
49-
nodir: true,
5042
});
5143

5244
let pathMapping = mapFilePaths(filePaths, {
@@ -56,11 +48,9 @@ function moveTypesFolder(options) {
5648

5749
moveFiles(pathMapping, options);
5850

59-
filePaths = globSync('types/**/*', {
51+
filePaths = findFiles('types/**/*', {
6052
cwd: projectRoot,
61-
dot: true,
62-
ignore: 'types/dummy/**/*',
63-
nodir: true,
53+
ignoreList: ['types/dummy/**/*'],
6454
});
6555

6656
pathMapping = mapFilePaths(filePaths, {
@@ -76,10 +66,8 @@ function renameDummy(options) {
7666

7767
// File extensions had been specified, partly to encode assumptions
7868
// about Ember, and partly to avoid corrupting non-text files
79-
const filePaths = globSync(`${locations.testApp}/**/*.{d.ts,html,js,ts}`, {
69+
const filePaths = findFiles(`${locations.testApp}/**/*.{d.ts,html,js,ts}`, {
8070
cwd: projectRoot,
81-
dot: true,
82-
nodir: true,
8371
});
8472

8573
filePaths.forEach((filePath) => {

src/migration/ember-addon/steps/update-addon-package-json.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ function updateOtherFields(packageJson, context, options) {
108108
packageJson['exports'] = {
109109
'.': './dist/index.js',
110110
'./*': {
111+
/*
112+
This object has an order dependency. The `default` key must appear last.
113+
*/
111114
types: './dist/*.d.ts',
112115
default: './dist/*.js',
113116
},

0 commit comments

Comments
 (0)