Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Convert asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeyax committed Mar 9, 2022
1 parent 8e8cd57 commit 95cef61
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ convert({
// convert multiple files & write them to disk
convert({
source: ['./src/**/*{.js,.ts}', './bin/main.ts'],
onConverted: (file) => file.saveSync()
});

// convert multiple files from another TypeScript project & write them to disk
// convert multiple files from a TypeScript project & asynchronously write them to disk
convert({
sourceTsConfig: '/path/to/project/tsconfig.json',
});
onConverted: async (file) => file.save()
})
.then(() => console.log('All files written successfully!'))
.catch(console.error);
```

### CLI
Expand Down
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"commander": "^9.0.0",
"is-promise": "^4.0.0",
"ts-morph": "^13.0.3"
},
"devDependencies": {
Expand Down
24 changes: 21 additions & 3 deletions src/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import {runInNewContext as evalInVm} from 'vm';

const testData = './tests/data/';

const convertCode = (code: string, options?: Omit<Options, 'onConverted'>) => {
const convertCode = (code: string, options?: Options) => {
let result = '';
convert({...options, sourceCode: code, onConverted: (file) => result += file.getFullText()});
convert({...options, sourceCode: code, onConverted: (file) => {
result += file.getFullText();
}});
return result;
}

const convertFiles = (files: string | readonly string[]) => {
let output = '';
convert({source: files, onConverted: (file) => output += file.getFullText()})
convert({source: files, onConverted: (file) => {
output += file.getFullText()
}})
return output;
};

Expand Down Expand Up @@ -253,3 +257,17 @@ test('it should convert parenthesis', () => {
expect(cmp(input, output, true)).toBe(true);
}
});

test('it should support async', async () => {
const expected = 'Big(1).plus(1)';
let actual = '';
await convert({
sourceCode: '1 + 1',
onConverted: async (file) => {
const p = Promise.resolve(1);
await p;
actual = file.getText();
},
});
expect(actual).toBe(expected);
});
55 changes: 31 additions & 24 deletions src/converter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Node, Project, SourceFile, SyntaxKind } from "ts-morph";
import { isPromise } from 'util/types';
import { mapSyntaxKind } from './mappings';

export type Options = {
Expand Down Expand Up @@ -54,25 +55,34 @@ export type Options = {
* Thus, in order to fix the example above you should specify `{..., variables: ['total']}` in your `Options` object..
*/
variables?: string[];

/**
* Called when an input file has been transformed to Bigs.
*
* Saves the input file to disk by default.
*/
onConverted?: (file: SourceFile) => void;
};

/**
* Function to call when an input file has been converted to Bigs.
*/
type OnConverted = (file: SourceFile) => void | Promise<void>;

/**
* The name of the filename when raw source code was given via the `sourceCode` option.
*/
export const sourceCodeFileName = 'n2b-source.ts';

export class Converter {
private readonly options: Options;
private readonly project: Project;

constructor(options: Options) {
this.project = new Project();
this.options = options;

if (options.sourceCode)
this.project.createSourceFile(sourceCodeFileName, options.sourceCode);

if (options.source)
this.project.addSourceFilesAtPaths(options.source);

if (options.sourceTsConfig)
this.project.addSourceFilesFromTsConfig(options.sourceTsConfig);
}

private createBig(content: string | number) {
Expand Down Expand Up @@ -196,30 +206,27 @@ export class Converter {
});
}

convert() {
const project = new Project();

if (this.options.sourceCode)
project.createSourceFile(sourceCodeFileName, this.options.sourceCode);
convert(onConverted: OnConverted = (file) => file.saveSync()) {
const files = this.project.getSourceFiles();

if (this.options.source)
project.addSourceFilesAtPaths(this.options.source);

if (this.options.sourceTsConfig)
project.addSourceFilesFromTsConfig(this.options.sourceTsConfig);

if (!this.options.onConverted)
this.options.onConverted = (file) => file.saveSync();
for (const file of files) {
this.traverse(file);
onConverted(file);
}
}

const files = project.getSourceFiles();
async convertAsync(onConverted: OnConverted = (file) => file.save()) {
const files = this.project.getSourceFiles();

for (const file of files) {
this.traverse(file);
this.options.onConverted(file);
await onConverted(file);
}
}
}

export function convert(options: Options) {
return new Converter(options).convert();
export function convert({onConverted, ...options}: Options & {onConverted?: OnConverted}) {
const converter = new Converter(options);
const async = isPromise(onConverted);
return async ? converter.convertAsync(onConverted) : converter.convert(onConverted);
}

0 comments on commit 95cef61

Please sign in to comment.