Skip to content

Commit

Permalink
Merge pull request #3 from Gozala/project-refs
Browse files Browse the repository at this point in the history
rewrite the reporter
  • Loading branch information
Gozala committed Oct 17, 2020
2 parents 15fee74 + 5840561 commit 28a7d97
Show file tree
Hide file tree
Showing 21 changed files with 290 additions and 847 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
lib
*.log
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: 'Report type check result on PR diff view.'
author: 'andoshin11'
runs:
using: 'node12'
main: 'dist/index.js'
main: 'lib/index.js'
branding:
icon: 'check-circle'
color: 'blue'
Expand Down
16 changes: 0 additions & 16 deletions dist/index.js

This file was deleted.

116 changes: 116 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const module_1 = __importDefault(require("module"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const core_1 = require("@actions/core");
const reporter_1 = require("./reporter");
async function main() {
try {
const project = core_1.getInput('project') || 'tsconfig.json';
const projectPath = resolveProjectPath(path.resolve(process.cwd(), project));
if (projectPath == null) {
throw new Error(`No valid typescript project was not found at: ${projectPath}`);
}
typecheck(projectPath);
}
catch (e) {
console.error(e);
core_1.setFailed(e);
}
}
/**
* Attempts to resolve ts config file and returns either path to it or `null`.
*/
const resolveProjectPath = (projectPath) => {
try {
if (fs.statSync(projectPath).isFile()) {
return projectPath;
}
else {
const configPath = path.resolve(projectPath, "tsconfig.json");
return fs.statSync(configPath).isFile() ? configPath : null;
}
}
catch {
return null;
}
};
const typecheck = (projectPath) => {
const ts = loadTS(projectPath);
const json = ts.readConfigFile(projectPath, ts.sys.readFile);
const config = ts.parseJsonConfigFileContent(json.config, ts.sys, path.dirname(projectPath), undefined, path.basename(projectPath));
const errors = isIncrementalCompilation(config.options)
? performIncrementalCompilation(ts, projectPath)
: performCompilation(ts, config);
if (errors > 0) {
core_1.setFailed(`Found ${errors} errors!`);
}
};
const performIncrementalCompilation = (ts, projectPath) => {
const report = reporter_1.reporter(ts);
const host = ts.createSolutionBuilderHost(ts.sys, undefined, report, report);
const builder = ts.createSolutionBuilder(host, [projectPath], { noEmit: true });
return builder.build();
};
const performCompilation = (ts, config) => {
const report = reporter_1.reporter(ts);
const host = ts.createCompilerHost(config.options);
const program = ts.createProgram({
rootNames: config.fileNames,
options: config.options,
projectReferences: config.projectReferences,
configFileParsingDiagnostics: ts.getConfigFileParsingDiagnostics(config)
});
const configuration = program.getConfigFileParsingDiagnostics();
let all = [...program.getSyntacticDiagnostics()];
if (all.length === 0) {
all = [
...program.getOptionsDiagnostics(),
...program.getGlobalDiagnostics()
];
if (all.length == 0) {
all = [...program.getSemanticDiagnostics()];
}
}
const diagnostics = ts.sortAndDeduplicateDiagnostics(all);
diagnostics.forEach(report);
return all.length;
};
const isIncrementalCompilation = (options) => options.incremental || options.composite;
const loadTS = (projectPath) => {
try {
const require = module_1.default.createRequire(projectPath);
const ts = require('typescript');
console.log(`Using local typescript@${ts.version}`);
return ts;
}
catch (error) {
const ts = require('typescript');
console.log(`Failed to find project specific typescript, falling back to bundled typescript@${ts.version}`);
return ts;
}
};
main();
39 changes: 39 additions & 0 deletions lib/reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseLocation = exports.readProperties = exports.reporter = void 0;
const command_1 = require("@actions/core/lib/command");
exports.reporter = (ts) => (diagnostic) => {
switch (diagnostic.category) {
case ts.DiagnosticCategory.Error: {
return command_1.issueCommand('error', exports.readProperties(diagnostic), ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
}
case ts.DiagnosticCategory.Warning: {
return command_1.issueCommand('warning', exports.readProperties(diagnostic), ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
}
}
};
exports.readProperties = ({ start, file }) => {
const fileName = file && file.fileName;
if (!fileName)
return {};
if (!start)
return { file: fileName };
const content = file.getFullText();
const { line, column } = exports.parseLocation(content, start);
return { file: fileName, line: `${line}`, col: `${column}` };
};
exports.parseLocation = (content, position) => {
let l = 1;
let c = 0;
for (let i = 0; i < content.length && i < position; i++) {
const cc = content[i];
if (cc === '\n') {
c = 0;
l++;
}
else {
c++;
}
}
return { line: l, column: c };
};
26 changes: 8 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
{
"name": "typescript-error-reporter-action",
"version": "1.0.0",
"main": "dist/index.js",
"main": "lib/index.js",
"author": "andoshin11 <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/andoshin11/typescript-error-reporter-action.git"
"url": "git+https://github.com/gozala/typescript-error-reporter-action.git"
},
"bugs": {
"url": "https://github.com/andoshin11/typescript-error-reporter-action/issues"
"url": "https://github.com/gozala/typescript-error-reporter-action/issues"
},
"homepage": "https://github.com/andoshin11/typescript-error-reporter-action#readme",
"homepage": "https://github.com/gozala/typescript-error-reporter-action#readme",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"bundle": "webpack",
"test:type": "tsc --noEmit --incremental false",
"pack:libDTS": "node scripts/packLibDTS.js"
"clean": "rm -rf lib",
"test:type": "tsc --noEmit --incremental false"
},
"devDependencies": {
"@types/concat-stream": "^1.6.0",
"@types/glob": "^7.1.3",
"@types/node": "^14.11.8",
"@types/yarnpkg__lockfile": "^1.1.4",
"hard-source-webpack-plugin": "^0.13.1",
"ts-loader": "^8.0.4",
"typescript": "^4.0.3",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12"
"typescript": "^4.0.3"
},
"dependencies": {
"@actions/core": "^1.2.6",
"@yarnpkg/lockfile": "^1.1.0",
"concat-stream": "^2.0.0",
"glob": "^7.1.6"
"@actions/core": "^1.2.6"
}
}
31 changes: 0 additions & 31 deletions scripts/packLibDTS.js

This file was deleted.

53 changes: 0 additions & 53 deletions src/doctor.ts

This file was deleted.

332 changes: 0 additions & 332 deletions src/gen/libDTS.ts

This file was deleted.

Loading

0 comments on commit 28a7d97

Please sign in to comment.