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 2 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
10 changes: 3 additions & 7 deletions src/migration/ember-addon/steps/create-files-from-blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

import { globSync } from 'glob';

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand Down Expand Up @@ -47,11 +45,9 @@ export function createFilesFromBlueprint(context, options) {

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
4 changes: 2 additions & 2 deletions src/migration/ember-addon/steps/create-options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

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

function analyzePackageJson(codemodOptions) {
const { projectRoot } = codemodOptions;
Expand Down Expand Up @@ -51,7 +51,7 @@ function analyzePackageJson(codemodOptions) {
function analyzePackageManager(codemodOptions) {
const { projectRoot } = codemodOptions;

const lockFiles = globSync('{package-lock.json,pnpm-lock.yaml,yarn.lock}', {
const lockFiles = findFiles('{package-lock.json,pnpm-lock.yaml,yarn.lock}', {
cwd: projectRoot,
});

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
11 changes: 5 additions & 6 deletions src/migration/ember-addon/steps/move-project-root-files.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { globSync } from 'glob';

import {
copyFiles,
findFiles,
mapFilePaths,
moveFiles,
removeFiles,
Expand All @@ -20,7 +19,7 @@ function copyToAddon(options) {

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

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

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

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

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

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

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

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

const filePaths = globSync(globPattern(files), {
const filePaths = findFiles(globPattern(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
10 changes: 3 additions & 7 deletions src/migration/ember-addon/steps/use-relative-paths.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync, writeFileSync } from 'node:fs';
import { dirname, join, relative } from 'node:path';

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

function normalizeRelativePath(relativePath) {
if (!relativePath.startsWith('..')) {
Expand Down Expand Up @@ -38,10 +38,8 @@ function useRelativePathInAddonFolder(options) {

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

filePaths.forEach((filePath) => {
Expand All @@ -63,10 +61,8 @@ function useRelativePathInTestsDummyFolder(options) {

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

filePaths.forEach((filePath) => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/files.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './files/copy-files.js';
export * from './files/create-files.js';
export * from './files/find-files.js';
export * from './files/map-file-paths.js';
export * from './files/move-files.js';
export * from './files/remove-directory-if-empty.js';
Expand Down
20 changes: 20 additions & 0 deletions src/utils/files/find-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { globSync } from 'glob';

export function findFiles(pattern, { cwd, ignoreList = [] }) {
if (!pattern) {
throw new RangeError('ERROR: The glob pattern is unknown.\n');
}

if (!cwd) {
throw new RangeError('ERROR: The current working directory is unknown.\n');
}

const filePaths = globSync(pattern, {
cwd,
dot: true,
ignore: ignoreList,
nodir: true,
});

return filePaths;
}
6 changes: 2 additions & 4 deletions tests/helpers/testing/convert-fixture-to-json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

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

function updateJson(json, { keys, projectRoot }) {
const key = keys.shift();
Expand Down Expand Up @@ -38,10 +38,8 @@ function createJson(filePaths = [], projectRoot) {
export function convertFixtureToJson(projectRoot) {
const absolutePath = `${process.cwd()}/tests/fixtures/${projectRoot}`;

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

return createJson(filePaths, absolutePath);
Expand Down