Skip to content

Commit

Permalink
update jsbeautify (for microsoft/vscode#53546)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Jul 9, 2018
1 parent df93251 commit 418f7c2
Show file tree
Hide file tree
Showing 8 changed files with 10,006 additions and 3,924 deletions.
87 changes: 31 additions & 56 deletions build/update-jsbeautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,50 @@

var path = require('path');
var fs = require('fs');
var https = require('https');
var url = require('url');

function getOptions(urlString) {
var _url = url.parse(urlString);
return {
protocol: _url.protocol,
host: _url.host,
port: _url.port,
path: _url.path,
headers: {
'User-Agent': 'NodeJS'
}
}
}

function download(url) {
return new Promise((c, e) => {
var content = '';
var request = https.get(getOptions(url), function (response) {
response.on('data', function (data) {
content += data.toString();
}).on('end', function () {
c(content);
});
}).on('error', function (err) {
e(err.message);
});
});
}

function getCommitSha(repoId, repoPath) {
var commitInfo = 'https://api.github.com/repos/' + repoId + '/commits?path=' + repoPath;
return download(commitInfo).then(function (content) {
function getVersion(moduleName) {
var packageJSONPath = path.join(__dirname, '..', 'node_modules', moduleName, 'package.json');
return readFile(packageJSONPath).then(function (content) {
try {
let lastCommit = JSON.parse(content)[0];
return Promise.resolve({
commitSha: lastCommit.sha,
commitDate: lastCommit.commit.author.date
});
return JSON.parse(content).version;
} catch (e) {
return Promise.resolve(null);
}
}, function () {
console.err('Failed loading ' + commitInfo);
return Promise.resolve(null);
});
}

function update(repoId, repoPath, dest, addHeader, patch) {
var contentPath = 'https://raw.githubusercontent.com/' + repoId + '/master/' + repoPath;
function readFile(path) {
return new Promise((s, e) => {
fs.readFile(path, (err, res) => {
if (err) {
e(err);
} else {
s(res.toString());
}
})
});

}

function update(moduleName, repoPath, dest, addHeader, patch) {
var contentPath = path.join(__dirname, '..', 'node_modules', moduleName, repoPath);
console.log('Reading from ' + contentPath);
return download(contentPath).then(function (content) {
return getCommitSha(repoId, repoPath).then(function (info) {
return readFile(contentPath).then(function (content) {
return getVersion(moduleName).then(function (version) {
let header = '';
if (addHeader) {
header = '// copied from ' + contentPath + '\n';
if (info) {
let version = 'https://github.com/' + repoId + '/commit/' + info.commitSha;
header += '// ' + version + '\n';
header = '// copied from js-beautify/' + repoPath + '\n';
if (version) {
header += '// version: ' + version + '\n';
}
}
try {
if (patch) {
content = patch(content);
}
fs.writeFileSync(dest, header + content);
if (info) {
console.log('Updated ' + path.basename(dest) + ' to ' + repoId + '@' + info.commitSha.substr(0, 7) + ' (' + info.commitDate.substr(0, 10) + ')');
if (version) {
console.log('Updated ' + path.basename(dest) + ' (' + version + ')');
} else {
console.log('Updated ' + path.basename(dest));
}
Expand All @@ -87,12 +62,12 @@ function update(repoId, repoPath, dest, addHeader, patch) {
}, console.error);
}

update('beautify-web/js-beautify', 'js/lib/beautify-html.js', './src/beautify/beautify-html.js', true);
update('beautify-web/js-beautify', 'js/lib/beautify-css.js', './src/beautify/beautify-css.js', true);
update('beautify-web/js-beautify', 'LICENSE', './src/beautify/beautify-license');
update('js-beautify', 'js/lib/beautify-html.js', './src/beautify/beautify-html.js', true);
update('js-beautify', 'js/lib/beautify-css.js', './src/beautify/beautify-css.js', true);
update('js-beautify', 'LICENSE', './src/beautify/beautify-license');

// ESM version
update('beautify-web/js-beautify', 'js/lib/beautify-html.js', './src/beautify/esm/beautify-html.js', true, function (contents) {
update('js-beautify', 'js/lib/beautify-html.js', './src/beautify/esm/beautify-html.js', true, function (contents) {
contents = contents.replace(
/\(function\(\) \{\nvar legacy_beautify_html/m,
`import { js_beautify } from "./beautify";
Expand All @@ -109,7 +84,7 @@ export function html_beautify(html_source, options) {

return contents;
});
update('beautify-web/js-beautify', 'js/lib/beautify-css.js', './src/beautify/esm/beautify-css.js', true, function (contents) {
update('js-beautify', 'js/lib/beautify-css.js', './src/beautify/esm/beautify-css.js', true, function (contents) {
contents = contents.replace(
/\(function\(\) \{\nvar legacy_beautify_css/m,
'export const css_beautify'
Expand Down
Loading

0 comments on commit 418f7c2

Please sign in to comment.