Skip to content

Commit

Permalink
Merge pull request #44 from criticalmanufacturing/development-Package…
Browse files Browse the repository at this point in the history
…PackerSupportForAsyncStartEndTokens

feat(packagePacker): Support for async tokens
  • Loading branch information
jsantos98 authored Jan 7, 2025
2 parents 8f385bc + 745e94b commit 0fdbb6d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
18 changes: 14 additions & 4 deletions generators/packagePacker/processors/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,24 @@ export class Transpiler {
const scriptFile = path.resolve(templateDirectory, pathMatch);
const scriptContent = io.readFileSync(scriptFile).toString();

const regexTokenMatcher = /\/\/\s+PackagePacker:\s+Start\s+of\s+Script([\s\S]*?)\/\/\s+PackagePacker:\s+End\s+of\s+Script/i;
const matches = scriptContent.match(regexTokenMatcher);
const regexTokenMatcherForPackagePacker = /\/\/\s+PackagePacker:\s+Start\s+of\s+Script([\s\S]*?)\/\/\s+PackagePacker:\s+End\s+of\s+Script/i;
const regexTokenMatcherForAsyncPackagePacker = /\/\/\s+PackagePacker:\s+Start\s+of\s+Async\s+Script([\s\S]*?)\/\/\s+PackagePacker:\s+End\s+of\s+Async\s+Script/i;

const matches = scriptContent.match(regexTokenMatcherForPackagePacker);
const matchesAsync = scriptContent.match(regexTokenMatcherForAsyncPackagePacker);
if (matches != null && matches.length === 2) {
// Check for hooks
// Start Hook -> // PackagePacker: Start of Script
// End Hook -> // PackagePacker: End of Script
return await this.transpile(matches[1], false);
} else {
return await this.transpile(matches[1].trim(), false);
} else if (matchesAsync != null && matchesAsync.length === 2) {
// Check for hooks and Add start '(async () => {' declaration and end '})();'
// Start Hook -> // PackagePacker: Start of Async Script
// End Hook -> // PackagePacker: End of Async Script
const script = "(async () => {\r\n" + matchesAsync[1].trim() + "\r\n})();";
return await this.transpile(script, false);
}
else {
return await this.transpile(scriptContent, false);
}
};
Expand Down
45 changes: 45 additions & 0 deletions generators/packagePacker/test/transpiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,51 @@ describe("Transpiler", () => {
});
expect(readFileSyncStub.calledOnce).to.be.true;
expect(transpileStub.calledOnce).to.be.true;
expect(transpileStub.calledWithExactly("dummy-script-content", false)).to.be.true;
expect(logStub.debug.calledOnce).to.be.true;
});

it("should process only values inside token", async () => {
const value = {
key1: "${script(some/path/script1.ts)}",
key2: "TestScript",
};
const templateDirectory = "/template/directory";

transpileStub = sinon.stub(transpiler, "transpile").resolves("transpiled-code");
readFileSyncStub = sinon.stub(io, "readFileSync").returns("*********// PackagePacker: Start of Script\r\ndummy-script-content\r\n// PackagePacker: End of Script\r\n*********");

const result = await transpiler.preProcessTaskScripts(templateDirectory, value);

expect(result).to.deep.equal({
key1: Buffer.from("transpiled-code").toString("base64"),
key2: "TestScript",
});
expect(readFileSyncStub.calledOnce).to.be.true;
expect(transpileStub.calledOnce).to.be.true;
expect(transpileStub.calledWithExactly("dummy-script-content", false)).to.be.true;
expect(logStub.debug.calledOnce).to.be.true;
});

it("should process only values inside the async token", async () => {
const value = {
key1: "${script(some/path/script1.ts)}",
key2: "TestScript",
};
const templateDirectory = "/template/directory";

transpileStub = sinon.stub(transpiler, "transpile").resolves("transpiled-code");
readFileSyncStub = sinon.stub(io, "readFileSync").returns("*********// PackagePacker: Start of Async Script\r\ndummy-script-content\r\n// PackagePacker: End of Async Script\r\n*********");

const result = await transpiler.preProcessTaskScripts(templateDirectory, value);

expect(result).to.deep.equal({
key1: Buffer.from("transpiled-code").toString("base64"),
key2: "TestScript",
});
expect(readFileSyncStub.calledOnce).to.be.true;
expect(transpileStub.calledOnce).to.be.true;
expect(transpileStub.calledWithExactly("(async () => {\r\ndummy-script-content\r\n})();", false)).to.be.true;
expect(logStub.debug.calledOnce).to.be.true;
});

Expand Down

0 comments on commit 0fdbb6d

Please sign in to comment.