elm-review/create-dependency/index.js

84 lines
2.1 KiB
JavaScript
Raw Normal View History

/* To run this script:
2021-03-12 19:49:19 +03:00
- Clone `elm-review` locally
Then run
npm run generate-dep -- <dependency>
Example:
npm run generate-dep -- elm/url
*/
2021-03-12 02:19:39 +03:00
const path = require('path');
2021-03-11 22:53:34 +03:00
const https = require('https');
2021-03-12 02:19:39 +03:00
const fs = require('fs').promises;
2021-03-12 19:33:54 +03:00
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function runElmMake() {
process.chdir('create-dependency/');
await exec('npx elm make src/DependencyCreator.elm --output elm-stuff/app.js');
process.chdir('..');
}
2021-03-11 22:53:34 +03:00
function get(url) {
return new Promise((resolve, reject) => {
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}).on("error", reject);
})
}
const packageName = process.argv[2];
if (!packageName) {
console.error(`Need to pass in a package name. For instance:
2021-03-12 19:33:54 +03:00
npm run generate-dep -- <dependency>
Example:
npm run generate-dep -- elm/url
2021-03-11 22:53:34 +03:00
`)
process.exit(1);
2021-03-11 22:53:34 +03:00
}
async function downloadFiles() {
const [elmJson, docsJson] = await Promise.all([
get(`https://package.elm-lang.org/packages/${packageName}/latest/elm.json`),
get(`https://package.elm-lang.org/packages/${packageName}/latest/docs.json`)
]);
return [elmJson, docsJson];
}
2021-03-12 19:36:16 +03:00
async function createFile([elmJson, docsJson]) {
await runElmMake();
const oldWarn = console.warn;
console.warn = () => { }
const app = require('./elm-stuff/app.js').Elm.DependencyCreator.init({
2021-03-11 22:53:34 +03:00
flags: { elmJson, docsJson }
});
2021-03-12 19:36:16 +03:00
console.warn = oldWarn;
2021-03-11 22:53:34 +03:00
2021-03-12 02:19:39 +03:00
app.ports.sendToJs.subscribe(async ([filePath, source]) => {
const relativeFilePath = path.resolve(process.cwd(), filePath);
await fs.mkdir(path.dirname(relativeFilePath), { recursive: true });
await fs.writeFile(relativeFilePath, source);
2021-03-12 19:39:03 +03:00
await exec('npx elm-format --yes ' + relativeFilePath);
2021-03-12 19:46:53 +03:00
console.log("File created! You can find it at:\n ", relativeFilePath);
2021-03-11 22:53:34 +03:00
});
}
downloadFiles()
.then(createFile)
.catch(console.error);