Skip to content

Commit

Permalink
Add support for directories in import script
Browse files Browse the repository at this point in the history
  • Loading branch information
undyingwraith committed Jan 22, 2025
1 parent 9b2536f commit 3f74084
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/core/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { createFilter } from './createFilter';
export { createRemoteIpfs } from './createRemoteIpfs';
export { partitionArray } from './partitionArray';
export { uuid } from './uuid';
12 changes: 12 additions & 0 deletions packages/core/src/util/partitionArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Partitions an array into two.
* @param array Array to split.
* @param filter Filter to split by.
* @returns [matches, non matches]
*/
export function partitionArray<TData>(array: TData[], filter: (value: TData, index: number, array: TData[]) => boolean): [TData[], TData[]] {
const pass: TData[] = [];
const fail: TData[] = [];
array.forEach((e, idx, arr) => (filter(e, idx, arr) ? pass : fail).push(e));
return [pass, fail];
}
6 changes: 5 additions & 1 deletion packages/core/src/util/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export function uuid() {
/**
* Generates a uuid.
* @returns a new uuid.
*/
export function uuid(): string {
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
(+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
);
Expand Down
56 changes: 34 additions & 22 deletions packages/tools/src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { exec } from 'child_process';
import { input, confirm, select } from '@inquirer/prompts';
import { ITempDir, tempDir } from './utils/tempDIr';
import chalk from 'chalk';
import { Regexes } from 'ipmc-core';
import { partitionArray, Regexes } from 'ipmc-core';
import path, { basename } from 'path';
import srt2vtt from 'srt2vtt';
import fs from 'fs';
Expand Down Expand Up @@ -115,25 +115,7 @@ async function getEpisodeMetadata(files: string[]): Promise<{ seriesTitle: strin
};
}

const args = yargs(hideBin(process.argv))
.option('packager', {
alias: 'p',
describe: 'packager executable to use',
default: 'shaka-packager',
})
.option('ipfs', {
describe: 'ipfs api url',
default: 'http://127.0.0.1:5002/api/v0'
})
.array('file')
.alias('file', 'f')
.demandOption(['file'])
.help()
.parseSync();

(async () => {
const files = args.file as string[];

async function importFiles(files: string[]) {
const temp = tempDir();
const outDir = tempDir();

Expand Down Expand Up @@ -205,11 +187,41 @@ const args = yargs(hideBin(process.argv))
console.log(`Item ${chalk.blue(metaData.fileName)}`);
}
}

console.log('Done!');
} finally {
outDir.clean();
temp.clean();
}
}

const args = yargs(hideBin(process.argv))
.option('packager', {
alias: 'p',
describe: 'packager executable to use',
default: 'shaka-packager',
})
.option('ipfs', {
describe: 'ipfs api url',
default: 'http://127.0.0.1:5002/api/v0'
})
.array('file')
.alias('file', 'f')
.demandOption(['file'])
.help()
.parseSync();

(async () => {
const paths = args.file as string[];

const [directories, files] = partitionArray(paths, path => fs.lstatSync(path).isDirectory());

for (const dir of directories) {
await importFiles(fs.readdirSync(dir).map(name => path.join(dir, name)));
}

if (files.length < 0) {
await importFiles(files);
}

console.log('Done!');
})();

0 comments on commit 3f74084

Please sign in to comment.