Skip to content

Commit 3372e29

Browse files
committed
Fixed writing files.
1 parent 9b6cb6a commit 3372e29

File tree

10 files changed

+269188
-59
lines changed

10 files changed

+269188
-59
lines changed

src/bin/handlers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ bus.on('transform.file.done', (filePath: string) => {
3737
child.send({message: 'fileEnd', payload: filePath});
3838
});
3939

40+
bus.on('write.start', (num: number) => {
41+
child.send({message: 'writeStart', payload: num});
42+
});
43+
44+
bus.on('write.end', (num: number) => {
45+
child.send({message: 'writeEnd', payload: num});
46+
});
47+
48+
bus.on('write.file.start', (filePath: string) => {
49+
child.send({message: 'fileWriteStart', payload: filePath});
50+
});
51+
52+
bus.on('transform.file.done', (filePath: string) => {
53+
child.send({message: 'fileWriteEnd', payload: filePath});
54+
});
55+
4056
// process.on('SIGINT', () => {
4157
// child.send({message: 'term'});
4258
// });

src/bin/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,44 @@
33
import './handlers';
44
import * as program from 'commander';
55
import transform from '../transform';
6+
import { Config } from '../config';
7+
import { CompilerResult } from '../compiler';
8+
import { Writer, WriterConfig, DEFAULT_CONFIG as DEFAULT_WRITER_CONFIG } from '../writer';
69
import { bus } from '../bus';
710
import * as status from './status';
811

912
const pkg = require('../../package.json');
13+
const config: Config = {};
14+
const writerConfig: WriterConfig = DEFAULT_WRITER_CONFIG;
15+
let write: boolean = false;
1016

11-
function defaultAction(env: any, options: any) {
17+
function defaultAction() {
1218
const files: string[] = program.args.filter((value) => {
1319
return typeof value === 'string';
1420
});
1521

16-
transform(files);
22+
transform(files)
23+
.then((compilerResult: CompilerResult) => {
24+
if (write) {
25+
const writer = new Writer(compilerResult);
26+
writer.writeAll();
27+
}
28+
});
1729
}
1830

1931
function setNoAssertConst() {
20-
32+
config.assertConst = true;
2133
}
2234

2335
function setEncoding(encoding: string) {
36+
config.encoding = encoding;
37+
writerConfig.encoding = encoding;
38+
}
2439

40+
function setWrite(location: string, base?: string) {
41+
write = true;
42+
writerConfig.writePath = location || writerConfig.writePath;
43+
writerConfig.basePath = base || writerConfig.basePath;
2544
}
2645

2746
program
@@ -31,10 +50,9 @@ program
3150
emits pretty printed TypeScript.
3251
--------------------------------`)
3352
.usage('[options] <file ...>')
34-
.option('-w, --write <location> [base]', 'persist files')
53+
.option('-w, --write', 'persist files', setWrite)
3554
.option('-e, --encoding <encoding>', 'set file encoding. defaults to utf8', setEncoding)
3655
.option('--no-assert-const', 'turn off const declaration checks.', setNoAssertConst)
37-
.action(defaultAction)
3856
.on('--help', () => {
3957
console.log(' Examples:');
4058
console.log();
@@ -47,4 +65,6 @@ program.parse(process.argv);
4765

4866
if (!process.argv.slice(2).length) {
4967
program.outputHelp();
68+
} else {
69+
defaultAction();
5070
}

src/bin/status.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,45 @@ status.term = () => {
6868
status.fileStart = (filePath: string) => {
6969
processed++;
7070
const countStr = chalk.gray(`[${processed}/${fileCount}]`);
71-
spinner.text = `${countStr} Processing ${filePath}`;
71+
spinner.text = `${countStr} Processing ${path.basename(filePath)}`;
7272
};
7373

7474
status.fileEnd = (filePath: string) => {
7575
const countStr = chalk.gray(`[${processed}/${fileCount}]`);
76-
spinner.succeed(`${countStr} Done processing ${filePath}`);
76+
spinner.succeed(`${countStr} Done processing ${path.basename(filePath)}`);
7777
spinner.start();
7878
};
7979

8080
status.fileFail = (filePath: string) => {
8181
errors++;
8282
const countStr = chalk.gray(`[${processed}/${fileCount}]`);
83-
spinner.fail(`${countStr} Error processing ${filePath}`);
83+
spinner.fail(`${countStr} Error processing ${path.basename(filePath)}`);
8484
spinner.start();
8585
};
8686

8787
status.fileReadError = (filePath: string) => {
8888
errors++;
8989
const countStr = chalk.gray(`[${processed}/${fileCount}]`);
90-
spinner.fail(`${countStr} Could not read ${filePath}`);
90+
spinner.fail(`${countStr} Could not read ${path.basename(filePath)}`);
9191
spinner.start();
9292
};
9393

94+
status.writeStart = (num: number) => {
95+
spinner.text = `Writing files`;
96+
};
97+
98+
status.writeEnd = (num: number) => {
99+
spinner.succeed(`Wrote ${num} files`);
100+
};
101+
102+
status.fileWriteStart = (filePath: string) => {
103+
104+
};
105+
106+
status.fileWriteEnd = (filePath: string) => {
107+
108+
};
109+
94110
status.error = (err: any) => {
95111
errors++;
96112

src/compiler/Compiler.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from 'fs';
1+
// import * as fs from 'fs';
22
import * as path from 'path';
33
import * as ts from 'typescript';
44
import { Config } from '../config';
@@ -76,17 +76,15 @@ export class Compiler {
7676

7777
return new Promise((resolve, reject) => {
7878

79+
filePath = path.normalize(path.join(process.cwd(), filePath));
80+
7981
const fileName = path.basename(filePath);
8082

8183
const source = ts.sys.readFile(filePath, this.config.encoding);
8284

8385
if (source === undefined) {
8486
bus.emit('transform.file.readError', filePath);
85-
return reject({
86-
fileName,
87-
filePath,
88-
result: undefined,
89-
});
87+
return reject(`Error reading ${filePath}`);
9088
}
9189

9290
let sourceFile = ts.createSourceFile(

src/playground/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import * as path from 'path';
22
import * as tsr from '../index';
33
import { Config } from '../config';
4-
import { Writer, WriterConfig } from '../writer';
4+
// import { Writer, WriterConfig } from '../writer';
55

66
const file1 = path.join(__dirname, 'test1.ts');
77
const file2 = path.join(__dirname, 'test.ts');
88

9-
const writerConfig: WriterConfig = {
10-
basePath: __dirname,
11-
writePath: __dirname,
12-
};
9+
// const writerConfig: WriterConfig = {
10+
// basePath: __dirname,
11+
// writePath: __dirname,
12+
// };
1313

14-
tsr.transform([file1, file2]).then(compilerResult => {
15-
const writer = new Writer(compilerResult);
16-
writer.writeAll(writerConfig);
17-
});
14+
tsr.transform([file1, file2]);
15+
16+
// .then(compilerResult => {
17+
// const writer = new Writer(compilerResult);
18+
// writer.writeAll(writerConfig);
19+
// });

src/playground/test.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)