Skip to content

Commit

Permalink
add an outputFilename option for strategies
Browse files Browse the repository at this point in the history
Strategies can now optionally define outputFilename in crawlPath.

For the following crawl path.

{
  name: "get-tokenuri",
  extractor: {
    args: [resolve(env.DATA_DIR, "soundxyz-call-tokenuri-transformation")],
    outputFilename: "soundxyz-get-tokenuri"
  },
  transformer: {},
}

The results get-tokenuri will be written to soundxyz-get-tokenuri-extraction.

Closes #258
  • Loading branch information
il3ven committed Oct 4, 2022
1 parent ffe11f1 commit 94a65e8
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/lifecycle.mjs
Expand Up @@ -128,7 +128,13 @@ export function generatePath(name, type) {
return path.resolve(dataDir, `${name}-${type}`);
}

export function extract(strategy, worker, messageRouter, args = []) {
export function extract(
strategy,
worker,
messageRouter,
outputPath,
args = []
) {
return new Promise(async (resolve, reject) => {
let numberOfMessages = 0;
const type = "extraction";
Expand All @@ -151,12 +157,11 @@ export function extract(strategy, worker, messageRouter, args = []) {
}

if (result.write) {
const filePath = generatePath(strategy.module.name, type);
try {
appendFileSync(filePath, `${result.write}\n`);
appendFileSync(outputPath, `${result.write}\n`);
} catch (err) {
const error = new Error(
`Couldn't write to file after update. Filepath: "${filePath}", Content: "${result.write}"`
`Couldn't write to file after update. Filepath: "${outputPath}", Content: "${result.write}"`
);
error.code = EXTRACTOR_CODES.FAILURE;
clearInterval(interval);
Expand Down Expand Up @@ -205,12 +210,11 @@ export function extract(strategy, worker, messageRouter, args = []) {
}

if (result.write) {
const filePath = generatePath(strategy.module.name, type);
try {
appendFileSync(filePath, `${result.write}\n`);
appendFileSync(outputPath, `${result.write}\n`);
} catch (err) {
const error = new Error(
`Couldn't write to file after update. Filepath: "${filePath}", Content: "${result.write}"`
`Couldn't write to file after update. Filepath: "${outputPath}", Content: "${result.write}"`
);
error.code = EXTRACTOR_CODES.FAILURE;
messageRouter.off(`${strategy.module.name}-${type}`, callback);
Expand Down Expand Up @@ -285,6 +289,10 @@ export async function init(worker, crawlPath) {
for await (const strategy of segment) {
if (strategy.extractor) {
const extractStrategy = finder("extraction", strategy.name);
const outputPath = generatePath(
strategy.extractor.outputFilename ?? extractStrategy.module.name,
"extraction"
);
log(
`Starting extractor strategy with name "${
extractStrategy.module.name
Expand All @@ -294,6 +302,7 @@ export async function init(worker, crawlPath) {
extractStrategy,
worker,
messageRouter,
outputPath,
strategy.extractor.args
);
log(
Expand All @@ -310,7 +319,7 @@ export async function init(worker, crawlPath) {
strategy.transformer.args?.[0] ??
generatePath(transformStrategy.module.name, "extraction");
const outputPath = generatePath(
transformStrategy.module.name,
strategy.transformer.outputFilename ?? transformStrategy.module.name,
"transformation"
);
await transform(
Expand Down

0 comments on commit 94a65e8

Please sign in to comment.