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

Wrapped globSync and extracted blueprintRoot #24

Merged
merged 8 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 4 additions & 8 deletions src/migration/ember-addon/steps/analyze-addon.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { globSync } from 'glob';

import { decideVersion } from '../../../utils/blueprints.js';
import { renameDirectory } from '../../../utils/files.js';
import { findFiles, renameDirectory } from '../../../utils/files.js';

function getAppReexports(options) {
const { projectRoot } = options;

const filePaths = globSync('app/**/*.js', {
const filePaths = findFiles('app/**/*.js', {
cwd: projectRoot,
});

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

const filePaths = globSync('public/**/*', {
const filePaths = findFiles('public/**/*', {
cwd: projectRoot,
dot: true,
nodir: true,
});

return filePaths
Expand All @@ -49,7 +45,7 @@ function getPublicAssets(options) {
function getPublicEntrypoints(options) {
const { projectRoot } = options;

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

Expand Down
26 changes: 5 additions & 21 deletions src/migration/ember-addon/steps/create-files-from-blueprint.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';

import { globSync } from 'glob';

import { processTemplate } from '../../../utils/blueprints.js';
import { createFiles } from '../../../utils/files.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

function getBlueprintRoot() {
const codemodRoot = join(__dirname, '../../../..');

return join(codemodRoot, 'src/blueprints/ember-addon');
}
import { blueprintRoot, processTemplate } from '../../../utils/blueprints.js';
import { createFiles, findFiles } from '../../../utils/files.js';

function getFilePath(blueprintFilePath, options) {
const { locations } = options;
Expand Down Expand Up @@ -43,15 +31,11 @@ function getFilesToSkip(options) {
}

export function createFilesFromBlueprint(context, options) {
const blueprintRoot = getBlueprintRoot();

const filesToSkip = getFilesToSkip(options);

const blueprintFilePaths = globSync('**/*', {
const blueprintFilePaths = findFiles('**/*', {
cwd: blueprintRoot,
dot: true,
ignore: filesToSkip,
nodir: true,
ignoreList: filesToSkip,
});

const fileMapping = new Map(
Expand Down
57 changes: 36 additions & 21 deletions src/migration/ember-addon/steps/create-options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { globSync } from 'glob';
import { findFiles, unionize } from '../../../utils/files.js';

function validatePackageJson({ name, version }) {
if (!name) {
throw new SyntaxError('Package name is missing.');
}

if (name.includes('/')) {
// eslint-disable-next-line no-unused-vars
const [_scope, packageName] = name.split('/');

if (!packageName) {
throw new SyntaxError('Package name is missing.');
}
}

if (!version) {
throw new SyntaxError('Package version is missing.');
}
}

function analyzePackageJson(codemodOptions) {
const { projectRoot } = codemodOptions;
Expand All @@ -20,13 +39,7 @@ function analyzePackageJson(codemodOptions) {
version,
} = JSON.parse(packageJsonFile);

if (!name) {
throw new SyntaxError('Package name is missing.');
}

if (!version) {
throw new SyntaxError('Package version is missing.');
}
validatePackageJson({ name, version });

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

const lockFiles = globSync('{package-lock.json,pnpm-lock.yaml,yarn.lock}', {
const mapping = new Map([
['package-lock.json', 'npm'],
['pnpm-lock.yaml', 'pnpm'],
['yarn.lock', 'yarn'],
]);

const lockFiles = [...mapping.keys()];

const filePaths = findFiles(unionize(lockFiles), {
cwd: projectRoot,
});

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

return {
Expand All @@ -65,12 +86,12 @@ function analyzePackageManager(codemodOptions) {
};
}

const [lockFile] = lockFiles;
const packageManager = mapping.get(filePaths[0]);

return {
isNpm: lockFile === 'package-lock.json',
isPnpm: lockFile === 'pnpm-lock.yaml',
isYarn: lockFile === 'yarn.lock',
isNpm: packageManager === 'npm',
isPnpm: packageManager === 'pnpm',
isYarn: packageManager === 'yarn',
};
}

Expand All @@ -81,13 +102,7 @@ function deriveAddonLocation(addonPackage) {
}

// eslint-disable-next-line no-unused-vars
const [scope, packageName] = addonPackage.name.split('/');

if (!packageName) {
throw new SyntaxError(
`ERROR: In package.json, the package name \`${addonPackage.name}\` is not valid.`
);
}
Comment on lines -86 to -90
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ember-codemod-pod-to-octane, I realized that it's not a good idea to perform validation inside this function (concerns weren't separated well).

const [_scope, packageName] = addonPackage.name.split('/');

return packageName;
}
Expand Down
29 changes: 11 additions & 18 deletions src/migration/ember-addon/steps/move-addon-files.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { globSync } from 'glob';

import { mapFilePaths, moveFiles, removeFiles } from '../../../utils/files.js';
import {
findFiles,
mapFilePaths,
moveFiles,
removeFiles,
} from '../../../utils/files.js';

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

const filePaths = globSync('addon/**/*', {
const filePaths = findFiles('addon/**/*', {
cwd: projectRoot,
dot: true,
nodir: true,
});

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

const filePaths = globSync('addon-test-support/**/*', {
const filePaths = findFiles('addon-test-support/**/*', {
cwd: projectRoot,
dot: true,
nodir: true,
});

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

const filePaths = globSync('blueprints/**/*', {
const filePaths = findFiles('blueprints/**/*', {
cwd: projectRoot,
dot: true,
nodir: true,
});

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

const filePaths = globSync('public/**/*', {
const filePaths = findFiles('public/**/*', {
cwd: projectRoot,
dot: true,
nodir: true,
});

const pathMapping = mapFilePaths(filePaths, {
Expand All @@ -73,10 +68,8 @@ function movePublicFolder(options) {
function removeAppFolder(options) {
const { projectRoot } = options;

const filePaths = globSync('app/**/*', {
const filePaths = findFiles('app/**/*', {
cwd: projectRoot,
dot: true,
nodir: true,
});

removeFiles(filePaths, options);
Expand Down
20 changes: 6 additions & 14 deletions src/migration/ember-addon/steps/move-project-root-files.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { globSync } from 'glob';

import {
copyFiles,
findFiles,
mapFilePaths,
moveFiles,
removeFiles,
unionize,
} from '../../../utils/files.js';

function globPattern(files) {
if (files.length <= 1) {
return files.join(',');
}

return `{${files.join(',')}}`;
}

function copyToAddon(options) {
const { locations, projectRoot } = options;

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

const filePaths = globSync(globPattern(files), {
const filePaths = findFiles(unionize(files), {
cwd: projectRoot,
});

Expand Down Expand Up @@ -55,7 +47,7 @@ function moveToAddonAndTestApp(options) {
files.add('tsconfig.json');
}

const filePaths = globSync(globPattern([...files]), {
const filePaths = findFiles(unionize([...files]), {
cwd: projectRoot,
});

Expand Down Expand Up @@ -86,7 +78,7 @@ function moveToTestApp(options) {
'testem.js',
];

const filePaths = globSync(globPattern(files), {
const filePaths = findFiles(unionize(files), {
cwd: projectRoot,
});

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

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

const filePaths = globSync(globPattern(files), {
const filePaths = findFiles(unionize(files), {
cwd: projectRoot,
});

Expand Down
28 changes: 8 additions & 20 deletions src/migration/ember-addon/steps/move-test-app-files.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';

import { globSync } from 'glob';

import { mapFilePaths, moveFiles } from '../../../utils/files.js';
import { findFiles, mapFilePaths, moveFiles } from '../../../utils/files.js';

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

let filePaths = globSync('tests/dummy/**/*', {
let filePaths = findFiles('tests/dummy/**/*', {
cwd: projectRoot,
dot: true,
nodir: true,
});

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

moveFiles(pathMapping, options);

filePaths = globSync('tests/**/*', {
filePaths = findFiles('tests/**/*', {
cwd: projectRoot,
dot: true,
ignore: 'tests/dummy/**/*',
nodir: true,
ignoreList: ['tests/dummy/**/*'],
});

pathMapping = mapFilePaths(filePaths, {
Expand All @@ -43,10 +37,8 @@ function moveTypesFolder(options) {
return;
}

let filePaths = globSync('types/dummy/**/*', {
let filePaths = findFiles('types/dummy/**/*', {
cwd: projectRoot,
dot: true,
nodir: true,
});

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

moveFiles(pathMapping, options);

filePaths = globSync('types/**/*', {
filePaths = findFiles('types/**/*', {
cwd: projectRoot,
dot: true,
ignore: 'types/dummy/**/*',
nodir: true,
ignoreList: ['types/dummy/**/*'],
});

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

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

filePaths.forEach((filePath) => {
Expand Down
Loading