UNPKG

6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var formatter_1 = require("./formatter");
4var utils_1 = require("./utils");
5exports.parseJSON = utils_1.parseJSON;
6var fs = require("fs");
7var path = require("path");
8var base = require("./provider/base");
9var tsconfigjson = require("./provider/tsconfigjson");
10var editorconfig = require("./provider/editorconfig");
11var tslintjson = require("./provider/tslintjson");
12var vscodesettings = require("./provider/vscodesettings");
13var os_1 = require("os");
14var packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json")).toString());
15exports.version = packageJson.version;
16var Processor = /** @class */ (function () {
17 function Processor() {
18 this.optionModifiers = [];
19 this.postProcessors = [];
20 }
21 Processor.prototype.addOptionModify = function (modifier) {
22 this.optionModifiers.push(modifier);
23 };
24 Processor.prototype.processFormatCodeOptions = function (fileName, opts, formatSettings) {
25 var optionModifiers = this.optionModifiers.slice();
26 var next = function (formatSettings) {
27 if (optionModifiers.length === 0) {
28 return Promise.resolve(formatSettings);
29 }
30 var modifier = optionModifiers.shift();
31 var ret = modifier(fileName, opts, formatSettings);
32 return Promise.resolve(ret).then(function (formatSettings) { return next(formatSettings); });
33 };
34 return next(formatSettings);
35 };
36 Processor.prototype.addPostProcess = function (postProcessor) {
37 this.postProcessors.push(postProcessor);
38 };
39 Processor.prototype.postProcess = function (fileName, formattedCode, opts, formatSettings) {
40 var postProcessors = this.postProcessors.slice();
41 var next = function (formattedCode) {
42 if (postProcessors.length === 0) {
43 return Promise.resolve(formattedCode);
44 }
45 var processor = postProcessors.shift();
46 var ret = processor(fileName, formattedCode, opts, formatSettings);
47 return Promise.resolve(ret).then(function (formattedCode) { return next(formattedCode); });
48 };
49 return next(formattedCode);
50 };
51 return Processor;
52}());
53function processFiles(files, opts) {
54 var resultMap = {};
55 var promises = files.map(function (fileName) {
56 if (!fs.existsSync(fileName)) {
57 var result = {
58 fileName: fileName,
59 settings: null,
60 message: fileName + " does not exist. process abort.\n",
61 error: true,
62 src: "",
63 dest: "",
64 };
65 return Promise.resolve(result);
66 }
67 var content = fs.readFileSync(fileName).toString();
68 return processString(fileName, content, opts);
69 });
70 return Promise.all(promises).then(function (resultList) {
71 resultList.forEach(function (result) {
72 resultMap[result.fileName] = result;
73 });
74 return resultMap;
75 });
76}
77exports.processFiles = processFiles;
78function processStream(fileName, input, opts) {
79 input.setEncoding("utf8");
80 var promise = new Promise(function (resolve, _reject) {
81 var fragment = "";
82 input.on("data", function (chunk) {
83 fragment += chunk;
84 });
85 input.on("end", function () {
86 resolve(fragment);
87 });
88 });
89 return promise.then(function (content) { return processString(fileName, content, opts); });
90}
91exports.processStream = processStream;
92function processString(fileName, content, opts) {
93 var processor = new Processor();
94 if (opts.tsfmt) {
95 processor.addOptionModify(base.makeFormatCodeOptions);
96 }
97 if (opts.tsconfig) {
98 processor.addOptionModify(tsconfigjson.makeFormatCodeOptions);
99 }
100 if (opts.editorconfig) {
101 processor.addOptionModify(editorconfig.makeFormatCodeOptions);
102 processor.addPostProcess(editorconfig.postProcess);
103 }
104 if (opts.tslint) {
105 processor.addOptionModify(tslintjson.makeFormatCodeOptions);
106 processor.addPostProcess(tslintjson.postProcess);
107 }
108 if (opts.vscode) {
109 processor.addOptionModify(vscodesettings.makeFormatCodeOptions);
110 }
111 processor.addPostProcess(function (_fileName, formattedCode, _opts, formatSettings) {
112 // replace newline code. maybe NewLineCharacter params affect to only "new" newline by language service.
113 formattedCode = formattedCode.replace(/\r?\n/g, formatSettings.newLineCharacter || os_1.EOL);
114 return Promise.resolve(formattedCode);
115 });
116 var formatSettings = utils_1.createDefaultFormatCodeSettings();
117 return processor.processFormatCodeOptions(fileName, opts, formatSettings)
118 .then(function (formatSettings) {
119 var formattedCode = formatter_1.format(fileName, content, formatSettings);
120 // apply post process logic
121 return processor.postProcess(fileName, formattedCode, opts, formatSettings);
122 }).then(function (formattedCode) {
123 var message = "";
124 var error = false;
125 if (opts && opts.verify) {
126 if (content !== formattedCode) {
127 message = fileName + " is not formatted\n";
128 error = true;
129 }
130 }
131 else if (opts && opts.replace) {
132 if (content !== formattedCode) {
133 fs.writeFileSync(fileName, formattedCode);
134 message = "replaced " + fileName + "\n";
135 }
136 }
137 else if (opts && !opts.dryRun) {
138 message = formattedCode;
139 }
140 var result = {
141 fileName: fileName,
142 settings: formatSettings,
143 message: message,
144 error: error,
145 src: content,
146 dest: formattedCode,
147 };
148 return Promise.resolve(result);
149 });
150}
151exports.processString = processString;
152//# sourceMappingURL=index.js.map
\No newline at end of file