1/**
2 * Tool to generate vol JSON fixture from a project.
3 *
4 * Usage: npx ts-node scripts/createFixture /path/to/app /path/to/output.json
5 */
6import realFS from 'fs';
7import glob from 'glob';
8import { fs, vol } from 'memfs';
9import path from 'path';
10
11function globAsync(pattern: string, options: Parameters<typeof glob>[1]): Promise<string[]> {
12  return new Promise((resolve, reject) => {
13    glob(pattern, options, (err, matches) => {
14      if (err != null) {
15        reject(err);
16      } else {
17        resolve(matches);
18      }
19    });
20  });
21}
22
23async function createFixtureAsync(targetDir: string, outputFile: string) {
24  const files = await globAsync('**/*', {
25    cwd: targetDir,
26    ignore: [
27      // binary files
28      '**/*.{jpg,png}',
29
30      // lock files
31      '**/*.lock',
32
33      // node files
34      '**/node_modules/**',
35
36      // generated files
37      '**/build/**',
38
39      // ios files
40      'ios/Pods/**',
41      'vendor/**',
42      '**/xcuserdata/**',
43      '**/*.xcassets/**',
44      '**/IDEWorkspaceChecks.plist',
45
46      // android files
47      '**/*.jar',
48      '**/*.keystore',
49      '**/gradlew',
50      '**/gradlew.bat',
51      '**/gradle/wrapper/**',
52    ],
53    nodir: true,
54  });
55  for (const file of files) {
56    const content = realFS.readFileSync(path.join(targetDir, file), 'utf8');
57    fs.mkdirSync(path.join('/', path.dirname(file)), { recursive: true });
58    fs.writeFileSync(path.join('/', file), content);
59  }
60  const resultJSON: Record<string, string | null> = {};
61  for (const [key, value] of Object.entries(vol.toJSON())) {
62    resultJSON[path.join('/app', path.relative('/', key))] = value;
63  }
64  realFS.writeFileSync(outputFile, JSON.stringify(resultJSON, null, 2));
65}
66
67(async () => {
68  if (process.argv.length !== 4) {
69    console.log(`Usage: ${path.basename(process.argv[1])} targetDir outputFile`);
70    process.exit(1);
71  }
72  const targetDir = process.argv[2];
73  const outputFile = process.argv[3];
74
75  try {
76    await createFixtureAsync(targetDir, outputFile);
77  } catch (e) {
78    console.error('Uncaught Error', e);
79    process.exit(1);
80  }
81})();
82