Created
September 11, 2018 11:47
-
-
Save embarq/4c1927f9ffc2816a4cd263bafc81210d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const path = require('path'); | |
| const fs = require('fs'); | |
| const qs = require("querystring"); | |
| const http = require("https"); | |
| const { exec } = require('child_process'); | |
| const { promisify } = require('util'); | |
| const stackblitz = require('@stackblitz/sdk'); | |
| const execAsync = promisify(exec); | |
| const readFileAsync = promisify(fs.readFile); | |
| const writeFileAsync = promisify(fs.writeFile); | |
| const CWD = process.cwd(); | |
| /** | |
| * @param {string} std | |
| */ | |
| function processStd(std) { | |
| return std | |
| .toString() | |
| .split('\n') | |
| .filter(x => x !== ''); | |
| } | |
| execAsync(`find . -type f -not -path "*node_modules/**" -not -path "*.git/**"`) | |
| .then(std => { | |
| const contents = processStd(std.stdout) | |
| .map(filePath => { | |
| const absFilePath = path.resolve(CWD, filePath); | |
| return () => readFileAsync(absFilePath) | |
| .then(content => { | |
| return { | |
| path: absFilePath, | |
| content: content.toString() | |
| } | |
| }) | |
| }) | |
| .reduce( | |
| (promise, next) => { | |
| return promise | |
| .then(result => next() | |
| .then(res => [ ...result, res ]) | |
| .catch(err => console.error(err))) | |
| .catch(err => console.error(err)) | |
| }, | |
| Promise.resolve([]) | |
| ); | |
| return contents; | |
| }) | |
| .then(contents => contents.reduce( | |
| (aggr, file) => { | |
| aggr[`project[files][${ file.path }]`] = file.content; | |
| return aggr; | |
| }, | |
| {} | |
| )) | |
| .then(files => { | |
| createStackblitzProject({ | |
| ...files, | |
| 'project[template]': 'angular-cli' | |
| }); | |
| }) | |
| function createStackblitzProject(data) { | |
| const options = { | |
| "method": "POST", | |
| "host": "stackblitz.com", | |
| "path": "/run", | |
| "headers": { | |
| "Content-Type": "application/x-www-form-urlencoded", | |
| "Cache-Control": "no-cache" | |
| } | |
| }; | |
| const req = http.request(options, function (res) { | |
| const chunks = []; | |
| res.on("data", function (chunk) { | |
| chunks.push(chunk); | |
| }); | |
| res.on("end", function () { | |
| const body = Buffer.concat(chunks); | |
| console.log(body.toString()); | |
| }); | |
| }); | |
| req.write(qs.stringify(data)); | |
| req.end(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment