Skip to content

Commit

Permalink
Update the CI
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Dec 16, 2019
1 parent 89ff34f commit 5c070ad
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.TS_BOT_TOKEN }}

# Update search links from the origin repo before pushing to the upstream
- run: git clone --depth 1 https://github.com/microsoft/TypeScript.git
- run: git clone --depth 1 https://github.com/microsoft/TypeScript.git TypeScript
- run: npm i
- run: node scripts/convertRelativeLinksToHardcoded.js "**/*.md" --write
- run: rm -rf TypeScript
Expand Down
10 changes: 5 additions & 5 deletions learn/systems/systems-fourslash.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Fourslash automatically generates mocha tests based on files you put inside [`/t
for this lives in [`/src/testRunner/fourslashRunner.ts`][1]. This class is instantiated in
[`/src/testRunner/runner.ts`][2].

Fom here the main work all lives in [`/src/harness/foudslash.ts`][3] where we'll be spending the rest of this
Fom here the main work all lives in [`/src/harness/fourslash.ts`][3] where we'll be spending the rest of this
section. The initial entry point is [`runFourSlashTest`][4] but the work is in [`runFourSlashTestContent`][5].

This function first creates a virtual fs, uses [`parseTestData`][6] to fill up the virtual fs. `parseTestData`:
Expand Down Expand Up @@ -56,8 +56,8 @@ moment;`
[0]: https://github.com/microsoft/TypeScript/blob/master/src/testRunner/fourslashRunner.ts
[1]: https://github.com/microsoft/TypeScript/blob/master/tests/cases/fourslash
[2]: https://github.com/microsoft/TypeScript/blob/master/src/testRunner/runner.ts
[3]: https://github.com/microsoft/TypeScript/blob/master/src/harness/fourslash.ts
[4]: <src/harness/fourslash.ts - function runFourSlashTest(>
[5]: <src/harness/fourslash.ts - function runFourSlashTestContent(>
[5]: <src/harness/fourslash.ts - function parseTestData(>
[3]: https://github.com/microsoft/TypeScript/blob/master/src/harness/fourslashImpl.ts
[4]: <src/harness/fourslashImpl.ts - function runFourSlashTest(>
[5]: <src/harness/fourslashImpl.ts - function runFourSlashTestContent(>
[5]: <src/harness/fourslashImpl.ts - function parseTestData(>
<!-- prettier-ignore-end -->
86 changes: 50 additions & 36 deletions scripts/convertRelativeLinksToHardcoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,64 @@
// Write:
// node scripts/convertRelativeLinksToHardcoded.js scripts/fixtures/input.md --write

const glob = require("glob")
const {readFileSync, writeFileSync, existsSync} = require("fs")
const {join} = require("path")
const glob = require("glob");
const { readFileSync, writeFileSync, existsSync } = require("fs");
const { join } = require("path");
const { execSync } = require("child_process");
const escapeRegex = require("escape-regex-string");

if (!process.argv[2]) throw new Error("Did not include a glob for markdown files to change")
if (!process.argv[2]) throw new Error("Did not include a glob for markdown files to change");

// This can be anything
const write = process.argv[3] !== undefined
const write = process.argv[3] !== undefined;

const possibleTSRepo = ["../typescript-compiler", "../TypeScript", "TypeScript"]
let repoPath = possibleTSRepo.find(existsSync)
const possibleTSRepo = ["../typescript-compiler", "../TypeScript", "TypeScript"];
let repoPath = possibleTSRepo.find(f => existsSync(join(f, "package.json")));
if (!repoPath) throw new Error("Could not find a TypeScript repo");

const repoHead = execSync(`git rev-parse HEAD | cut -c 1-8`, { cwd: repoPath, encoding: "utf8" });
if (!repoHead) throw new Error("Could not get the git info from the sibling TypeScript repo")
if (!repoHead) throw new Error("Could not get the git info from the sibling TypeScript repo");

const files = glob.sync(process.argv[2])
if (!files.length) throw new Error("Did not get any files with that glob")
const files = glob.sync(process.argv[2]);
if (!files.length) throw new Error("Did not get any files with that glob");

let failed = [];

files.forEach(file => {
let content = readFileSync(file, "utf8")
if (file === "README.md") return;

let content = readFileSync(file, "utf8");
// https://regex101.com/r/w1dEG1/1
const regex = new RegExp(/\[.*]: <(.*) - (.*)>/g)
let result
while (result = regex.exec(content)) {
const file = result[1];
const regex = new RegExp(/\[.*]: <(.*) - (.*)>/g);

let result;
while ((result = regex.exec(content))) {
const fileRef = result[1];
const searchTerm = result[2];
const original = `: <${file} - ${searchTerm}>`
const originalFile = readFileSync(join(repoPath, file), "utf8")
if (!originalFile) throw new Error(`Could not find a file at '${join(repoPath, file)}'`)

const line = getLineNo(originalFile, new RegExp(escapeRegex(searchTerm)))
const lineRef = line && line[0] && line[0].number ? `#L${line[0].number}`: ""
const replacement = `: https://github.com/microsoft/TypeScript/blob/${repoHead.trim()}/${file}${lineRef}`
content = content.replace(original, replacement)
const original = `: <${fileRef} - ${searchTerm}>`;
try {
const originalFile = readFileSync(join(repoPath, fileRef), "utf8");
const line = getLineNo(originalFile, new RegExp(escapeRegex(searchTerm)));
const lineRef = line && line[0] && line[0].number ? `#L${line[0].number}` : "";
const replacement = `: https://github.com/microsoft/TypeScript/blob/${repoHead.trim()}/${fileRef}${lineRef}`;
content = content.replace(original, replacement);
} catch (e) {
failed.push([file, fileRef]);
}
}

if (write) {
writeFileSync(file, content)
writeFileSync(file, content);
} else {
console.log(content)
console.log(content);
}
});

if (failed.length) {
console.error("Could not find:");
console.error(failed);
process.exit(1);
}

/*!
* line-number <https://github.com/jonschlinkert/line-number>
Expand All @@ -67,13 +78,16 @@ files.forEach(file => {
* Licensed under the MIT License
*/
function getLineNo(str, re) {
return str.split(/\r?\n/).map(function (line, i) {
if (re.test(line)) {
return {
line: line,
number: i + 1,
match: line.match(re)[0]
};
}
}).filter(Boolean);
};
return str
.split(/\r?\n/)
.map(function(line, i) {
if (re.test(line)) {
return {
line: line,
number: i + 1,
match: line.match(re)[0]
};
}
})
.filter(Boolean);
}

0 comments on commit 5c070ad

Please sign in to comment.