From 3c72cee90a1b0d0804fe7df523d1a6cf9932fbaf Mon Sep 17 00:00:00 2001 From: Julian Pufler Date: Mon, 20 Jul 2020 06:34:59 +0200 Subject: [PATCH] Refactoring (#8) * replaced " by ' * no idea why it doesnt execute * it was the semicolons, alright * refactoring * fixed exclude pattern and made it async --- examples/discord-bot/index.ws | 4 +- examples/fizzbuzz/fizzbuzz.ws | 6 +- examples/slack-spotify-status/index.ws | 54 +++++++++--------- index.js | 78 +++++++++++++------------- package.json | 3 +- 5 files changed, 71 insertions(+), 74 deletions(-) diff --git a/examples/discord-bot/index.ws b/examples/discord-bot/index.ws index d652891..f6d98e5 100644 --- a/examples/discord-bot/index.ws +++ b/examples/discord-bot/index.ws @@ -19,10 +19,10 @@ client.on('message', OWIZAHRA (message) HUACH ZUA { FIX OIDA m WENNST MANST JO GLEI message.channel.send("Ping?"); m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`); PASST SCHO; - I SCHAU NUR "say": + I SCHAU NUR 'say': SCHAU MA MOL { JO GLEI message.delete(); - JO GLEI message.channel.send(args.join(" ")); + JO GLEI message.channel.send(args.join(' ')); } LECK OASCH (err) { GSCHISSN GRISSN(err) } diff --git a/examples/fizzbuzz/fizzbuzz.ws b/examples/fizzbuzz/fizzbuzz.ws index e661d9a..3abb0ac 100644 --- a/examples/fizzbuzz/fizzbuzz.ws +++ b/examples/fizzbuzz/fizzbuzz.ws @@ -1,10 +1,10 @@ STRAWANZ MA ( OIDA n WENNST MANST 1; n HOIT NET GRESSA 100; n WENNST MANST n AUFI 1 ) { - OIDA out WENNST MANST ""; + OIDA out WENNST MANST ''; WOS WÜSTN (JO EH(n S'RESTL 3)) - out WENNST MANST out AUFI "Fizz"; + out WENNST MANST out AUFI 'Fizz'; WOS WÜSTN (JO EH(n S'RESTL 5)) - out WENNST MANST out AUFI "Buzz"; + out WENNST MANST out AUFI 'Buzz'; I MAN JA NUR (out HOST MI out DANN HOIT NET n); } diff --git a/examples/slack-spotify-status/index.ws b/examples/slack-spotify-status/index.ws index 456c166..874db39 100644 --- a/examples/slack-spotify-status/index.ws +++ b/examples/slack-spotify-status/index.ws @@ -1,14 +1,14 @@ -require("dotenv").config(); -FIX OIDA { App, ExpressReceiver } WENNST MANST require("@slack/bolt"); -FIX OIDA axios WENNST MANST require("axios"); -FIX OIDA qs WENNST MANST require("querystring"); +require('dotenv').config(); +FIX OIDA { App, ExpressReceiver } WENNST MANST require('@slack/bolt'); +FIX OIDA axios WENNST MANST require('axios'); +FIX OIDA qs WENNST MANST require('querystring'); FIX OIDA expressReceiver WENNST MANST HAWIDERE ExpressReceiver({}); FIX OIDA app WENNST MANST HAWIDERE App({ signingSecret: process.env.SLACK_SIGNING_SECRET, token: process.env.SLACK_USER_TOKEN, receiver: expressReceiver, - logLevel: "DEBUG" + logLevel: 'DEBUG' }); FIX OIDA express WENNST MANST expressReceiver.app; @@ -17,8 +17,8 @@ FIX OIDA express WENNST MANST expressReceiver.app; FIX OIDA EMOJI WENNST MANST ':spotify:' // ping function to keep glitch alive -express.get("/ping", OWIZAHRA HACKL AMOI WOS (req, res) { - I MAN JA NUR ("<3"); +express.get('/ping', OWIZAHRA HACKL AMOI WOS (req, res) { + I MAN JA NUR ('<3'); FIX OIDA spotifyInfo WENNST MANST JO GLEI getSpotifyStatus(); WOS WÜSTN (spotifyInfo.is_playing) { @@ -26,7 +26,7 @@ express.get("/ping", OWIZAHRA HACKL AMOI WOS (req, res) { name: spotifyInfo.item.name, artists: spotifyInfo.item.artists .map(artist HUACH ZUA artist.name) - .join(" & ") + .join(' & ') }; JO GLEI setStatus(song, EMOJI); @@ -37,25 +37,25 @@ express.get("/ping", OWIZAHRA HACKL AMOI WOS (req, res) { // only unset status if it's a spotify status WOS WÜSTN (isSpotifyStatus(status)) JO GLEI unsetStatus(); } - DRAH DI HAM res.send({ ping: "pong" }); + DRAH DI HAM res.send({ ping: 'pong' }); }); -express.get("/spotify/connect", OWIZAHRA HACKL AMOI WOS (req, res) { - FIX OIDA scopes WENNST MANST "user-read-currently-playing user-read-playback-state"; +express.get('/spotify/connect', OWIZAHRA HACKL AMOI WOS (req, res) { + FIX OIDA scopes WENNST MANST 'user-read-currently-playing user-read-playback-state'; res.redirect( - "https://accounts.spotify.com/authorize" + - "?response_type=code" + - "&client_id=" + + 'https://accounts.spotify.com/authorize' + + '?response_type=code' + + '&client_id=' + process.env.SPOTIFY_CLIENT_ID + - (scopes ? "&scope=" + encodeURIComponent(scopes) : "") + - "&redirect_uri+" + + (scopes ? '&scope=' + encodeURIComponent(scopes) : '') + + '&redirect_uri+' + encodeURIComponent(process.env.SPOTIFY_REDIRECT_URL) ); }); -express.get("/spotify/oauth", OWIZAHRA HACKL AMOI WOS (req, res) { +express.get('/spotify/oauth', OWIZAHRA HACKL AMOI WOS (req, res) { FIX OIDA result WENNST MANST JO GLEI getSpotifyToken({ - grant_type: "authorization_code", + grant_type: 'authorization_code', code: req.query.code, redirect_uri: process.env.SPOTIFY_REDIRECT_URL }); @@ -64,19 +64,19 @@ express.get("/spotify/oauth", OWIZAHRA HACKL AMOI WOS (req, res) { HACKL AMOI WOS base64 (data) { FIX OIDA buff WENNST MANST new Buffer(data); - DRAH DI HAM buff.toString("base64"); + DRAH DI HAM buff.toString('base64'); }; OWIZAHRA HACKL AMOI WOS getSpotifyToken (body) { SCHAU MA MOL { FIX OIDA config WENNST MANST { headers: { - "Content-Type": "application/x-www-form-urlencoded", + 'Content-Type': 'application/x-www-form-urlencoded', Authorization: - "Basic " + + 'Basic ' + base64( process.env.SPOTIFY_CLIENT_ID + - ":" + + ':' + process.env.SPOTIFY_CLIENT_SECRET ) } @@ -95,13 +95,13 @@ OWIZAHRA HACKL AMOI WOS getSpotifyToken (body) { OWIZAHRA HACKL AMOI WOS getSpotifyStatus () { FIX OIDA token WENNST MANST JO GLEI getSpotifyToken({ - grant_type: "refresh_token", + grant_type: 'refresh_token', refresh_token: process.env.SPOTIFY_REFRESH_TOKEN }); FIX OIDA config WENNST MANST { headers: { - Authorization: "Bearer " + token.access_token + Authorization: 'Bearer ' + token.access_token } }; FIX OIDA result WENNST MANST JO GLEI axios.get( @@ -130,8 +130,8 @@ OWIZAHRA HACKL AMOI WOS unsetStatus () { JO GLEI app.client.users.profile.set({ token: process.env.SLACK_USER_TOKEN, profile: { - status_text: "", - status_emoji: "" + status_text: '', + status_emoji: '' } }); }; @@ -155,5 +155,5 @@ app.error(HACKL AMOI WOS (error) { (OWIZAHRA HACKL AMOI WOS () { JO GLEI app.start(process.env.PORT || 3000); - I MAN JA NUR ("⚡️ Bolt app is running!"); + I MAN JA NUR ('⚡️ Bolt app is running!'); })(); diff --git a/index.js b/index.js index efcbb33..e128524 100644 --- a/index.js +++ b/index.js @@ -1,47 +1,45 @@ #!/usr/bin/env node +const glob = require('glob'), + fs = require('fs'), + path = require('path'), + prettier = require('prettier'), + keywords = JSON.parse(fs.readFileSync('./keywords.json')); -const glob = require('glob') -const fs = require('fs') -const path = require('path') -const mkdirp = require('mkdirp') -const prettier = require('prettier') +const getFiles = async () => new Promise((resolve, reject) => + glob(process.cwd() + '/**/*.ws', { ignore: process.cwd() + '/node_modules/**'}, (err, matches) => err && reject(err) || matches && resolve(matches))); -// options is optional -const getFiles = (currentPath) => { - const files = glob.sync(currentPath + "/**/*.ws") - return files.filter(file => file.indexOf('node_modules/') < 0) -} +const getContent = (path) => new Promise((resolve, reject) => + fs.readFile(path, (err, data) => err && reject(err) || data && resolve(data))); -const compile = (content) => { - const keywords = JSON.parse(fs.readFileSync(path.join(__dirname, 'keywords.json'), 'utf8')) - for (const key in keywords) { - content = content.replace(new RegExp(key, "g"), keywords[key]); - } - return content -} +const transform = async (file) => ({ + path: path.join(process.cwd(), 'dist', file.replace(process.cwd(), '').replace(/\.ws$/, '.js')), + file: file.replace(process.cwd(), ''), + content: String(await getContent(file)) +}); -const run = () => { - console.log('HAWIDERE') - const currentPath = process.cwd() - const files = getFiles(currentPath) +const transpile = (file) => { + Object.entries(keywords).map(([key, value]) => (file = { + ...file, + content: file.content.replace(new RegExp(key, 'g'), value) + })); + return file; +}; - console.log('DO HOBN MA ' + files.length + ' GSCHICHTLN') +const saveFile = (file) => new Promise((resolve, reject) => { + fs.mkdir(file.path.substring(0, file.path.lastIndexOf(path.sep)), { recursive: true }, (err) => { + if (err) return reject(err); + console.info(`[WienerScript] DRAH DI DEPPATA ${file.file} ==> ${file.path.replace(process.cwd(), '')}`); + fs.writeFile(file.path, prettier.format(file.content, { parser: 'babel' }), (err, data) => err && reject(err) || data && resolve(data)); + }); +}); - files.forEach(file => { - file = path.normalize(file) - const content = fs.readFileSync(path.normalize(file), 'utf8') - const dir = path.join(currentPath, 'dist') - if (!fs.existsSync(dir)) fs.mkdirSync(dir) - - const dirs = file.substring(0, file.lastIndexOf(path.sep)).replace(currentPath, '') - mkdirp.sync(path.join(currentPath, 'dist', dirs)) - - const filePath = path.join(currentPath, 'dist', file.replace(currentPath, '').replace('.ws', '.js')) - console.log('DRAH DI DEPATTA', file.replace(currentPath, '').replace(path.sep, '') + ' ==> ' + filePath.replace(currentPath, '').replace(path.sep, '')) - fs.writeFileSync(filePath, prettier.format(compile(content), { parser: 'babel' }), (error) => console.error(error)) - }) - - console.log('PFIATI') -} - -run() \ No newline at end of file +(async () => { + try { + const files = (await getFiles()).map(path.normalize); + console.info(`[WienerScript] HAWIDERE! DO HOBN MA ${files.length} GSCHICHTLN!`); + (await Promise.all(files.map(transform))).map(transpile).map(saveFile); + } catch(err) { + console.error('[WienerScript] DO IS WOS SCHIEF GRENNT:'); + throw err; + } +})(); \ No newline at end of file diff --git a/package.json b/package.json index b46f354..3846020 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wienerscript", - "version": "1.0.2", + "version": "1.1.0", "description": "WienerScript adds syntactic WIENERISCH to JavaScript that makes it less forgiving, more emotional, and even painful to write, allowing you to do less with more code.", "main": "./index.js", "bin": { @@ -19,7 +19,6 @@ "license": "MIT", "dependencies": { "glob": "^7.1.6", - "mkdirp": "^1.0.4", "prettier": "^2.0.5" } }