Skip to content

Commit 5095bec

Browse files
committed
refactor: putout: cli: getFiles: migrate to ESM
1 parent da9ad5f commit 5095bec

File tree

6 files changed

+183
-225
lines changed

6 files changed

+183
-225
lines changed
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
'use strict';
2-
3-
const {normalize} = require('node:path');
4-
const {lstat: _lstat} = require('node:fs/promises');
5-
6-
const _fastGlob = require('fast-glob');
7-
const tryToCatch = require('try-to-catch');
8-
9-
const {getSupportedGlob: _getSupportedGlob} = require('./supported-files');
1+
import {normalize} from 'node:path';
2+
import {lstat as _lstat} from 'node:fs/promises';
3+
import _fastGlob from 'fast-glob';
4+
import tryToCatch from 'try-to-catch';
5+
import {getSupportedGlob as _getSupportedGlob} from './supported-files.js';
106

117
const rmDuplicates = (a) => Array.from(new Set(a));
128
const unixifyPath = (a) => !a.includes('\\') ? a : a.replace(/\\/g, '/');
139

14-
module.exports = async (args, options, overrides = {}) => {
10+
export const getFiles = async (args, options, overrides = {}) => {
1511
const {
1612
fastGlob = _fastGlob,
1713
lstat = _lstat,
1814
getSupportedGlob = _getSupportedGlob,
1915
} = overrides;
2016

21-
return await tryToCatch(getFiles, args, options, {
17+
return await tryToCatch(getFilesProcessor, args, options, {
2218
fastGlob,
2319
lstat,
2420
getSupportedGlob,
2521
});
2622
};
2723

28-
async function getFiles(args, options, overrides) {
24+
async function getFilesProcessor(args, options, overrides) {
2925
const promises = args.map(addExt(options, overrides));
3026
const files = await Promise.all(promises);
3127
const mergedFiles = files.flat();

packages/putout/lib/cli/get-files.spec.js renamed to packages/putout/lib/cli/get-files.spec.mjs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
2-
3-
const {join} = require('node:path');
4-
const fs = require('node:fs/promises');
5-
6-
const {test, stub} = require('supertape');
7-
8-
const getFiles = require('./get-files');
1+
import {join, dirname} from 'node:path';
2+
import {fileURLToPath} from 'node:url';
3+
import {test, stub} from 'supertape';
4+
import {getFiles} from './get-files.mjs';
95

6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
108
const rmStart = (a) => a.replace('lib/', '');
119

1210
test('putout: getFiles: error', async (t) => {
@@ -24,9 +22,7 @@ test('putout: getFiles: error: not first', async (t) => {
2422
});
2523

2624
test('putout: getFiles', async (t) => {
27-
const {lstat} = fs;
28-
29-
fs.lstat = stub().returns({
25+
const lstat = stub().returns({
3026
isDirectory: stub().returns(false),
3127
});
3228

@@ -35,6 +31,7 @@ test('putout: getFiles', async (t) => {
3531
const options = {};
3632
const [, files] = await getFiles(['**/get-files*.js'], options, {
3733
fastGlob,
34+
lstat,
3835
});
3936

4037
const result = files.map(rmStart);
@@ -44,8 +41,6 @@ test('putout: getFiles', async (t) => {
4441
'get-files.spec.js',
4542
];
4643

47-
fs.lstat = lstat;
48-
4944
t.deepEqual(result, expected);
5045
t.end();
5146
});

packages/putout/lib/cli/index.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ const {nanomemoize} = require('nano-memoize');
88
const tryCatch = require('try-catch');
99
const wraptile = require('wraptile');
1010
const fullstore = require('fullstore');
11+
const _cliStaged = require('@putout/cli-staged');
1112

1213
const {
1314
getFilePatterns,
1415
getProcessorRunners,
1516
defaultProcessors,
1617
} = require('@putout/engine-processor');
1718

19+
const _initReport = require('@putout/engine-reporter/report');
1820
const {keypress: _keypress} = require('@putout/cli-keypress');
1921

2022
const supportedFiles = require('./supported-files');
21-
const getOptions = require('./get-options');
23+
const _getOptions = require('./get-options');
2224
const {argvConfig, parseArgs} = require('./parse-args');
2325

24-
const getFiles = require('./get-files');
26+
const {getFiles: _getFiles} = require('./get-files.mjs');
2527
const {version, dependencies} = require('../../package.json');
2628
const {formatter: defaultFormatter} = require('../../putout.json');
2729
const {simpleImport} = require('./simple-import');
@@ -46,11 +48,12 @@ const {
4648
const noop = () => {};
4749
const {keys} = Object;
4850
const {isSupported} = supportedFiles;
49-
const getFormatter = nanomemoize(require('@putout/engine-reporter/formatter').getFormatter);
51+
const _getFormatter = nanomemoize(require('@putout/engine-reporter/formatter').getFormatter);
5052

5153
const cwd = process.cwd();
52-
const {PUTOUT_FILES = ''} = process.env;
53-
const envNames = !PUTOUT_FILES ? [] : PUTOUT_FILES.split(',');
54+
const {env} = process;
55+
56+
const getEnvNames = () => !env.PUTOUT_FILES ? [] : env.PUTOUT_FILES.split(',');
5457

5558
const getExitCode = (wasStop) => wasStop() ? WAS_STOP : OK;
5659
const isStr = (a) => typeof a === 'string';
@@ -76,6 +79,13 @@ module.exports = async (overrides = {}) => {
7679
writeFile,
7780
trace = noop,
7881
keypress = _keypress,
82+
getOptions = _getOptions,
83+
getFiles = _getFiles,
84+
cliStaged = _cliStaged,
85+
cliCache,
86+
initProcessFile,
87+
initReport = _initReport,
88+
getFormatter = _getFormatter,
7989
} = overrides;
8090

8191
const isStop = parseIsStop(overrides.isStop || noop, {
@@ -212,7 +222,7 @@ module.exports = async (overrides = {}) => {
212222
const stagedNames = [];
213223

214224
if (staged) {
215-
const {get} = require('@putout/cli-staged');
225+
const {get} = cliStaged;
216226
const {findUp} = await simpleImport('find-up');
217227

218228
const [error, names] = await tryToCatch(get, {
@@ -229,7 +239,7 @@ module.exports = async (overrides = {}) => {
229239
const globFiles = [
230240
...stagedNames,
231241
...args._.map(String),
232-
...envNames,
242+
...getEnvNames(),
233243
];
234244

235245
const [e, names] = await getFiles(globFiles, {
@@ -247,7 +257,7 @@ module.exports = async (overrides = {}) => {
247257
if (noFiles)
248258
return exit();
249259

250-
const {createCache} = await simpleImport('@putout/cli-cache');
260+
const {createCache} = cliCache || await simpleImport('@putout/cli-cache');
251261

252262
const fileCache = await createCache({
253263
version,
@@ -273,7 +283,10 @@ module.exports = async (overrides = {}) => {
273283
plugins,
274284
};
275285

286+
const report = initReport();
287+
276288
const {places, exited} = await run({
289+
report,
277290
processorRunners,
278291
trace,
279292
fix,
@@ -294,6 +307,7 @@ module.exports = async (overrides = {}) => {
294307
noConfig,
295308
plugins,
296309
transform,
310+
initProcessFile,
297311
});
298312

299313
if (exited)
@@ -313,7 +327,7 @@ module.exports = async (overrides = {}) => {
313327
}
314328

315329
if (fix && staged) {
316-
const {set} = require('@putout/cli-staged');
330+
const {set} = cliStaged;
317331
const {findUp} = await simpleImport('find-up');
318332

319333
const stagedNames = await set({

0 commit comments

Comments
 (0)