UNPKG

5.08 kBJavaScriptView Raw
1#!/usr/bin/env node
2function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var fs=_interopDefault(require("fs")),path=_interopDefault(require("path")),readline=_interopDefault(require("readline"));const argRegExp=/^--([\w-]+)$/,primativeRegExp=/^(false|null|true|undefined|(\d+\.)?\d+|\{.*\}|\[.*\])$/,relaxedJsonPropRegExp=/(['"])?([a-z0-9A-Z_]+)\1:/g,relaxedJsonValueRegExp=/("[a-z0-9A-Z_]+":\s*)(?!true|false|null|\d+)'?([A-z0-9]+)'?([,}])/g;function getOptionsFromArguments(e){return process.argv.slice(2).reduce((e,r,t,n)=>{const s=n[t+1],i=r.match(argRegExp);if(i){const[,r]=i;!s||argRegExp.test(s)?e[r]=!0:e[r]=primativeRegExp.test(s)?JSON.parse(s.replace(relaxedJsonPropRegExp,'"$2": ').replace(relaxedJsonValueRegExp,'$1"$2"$3')):s}return e},Object.assign({},e))}function readFile(e){return new Promise((r,t)=>{fs.readFile(e,"utf8",(e,n)=>{e?t(e):r(n)})})}function readJSON(e,...r){return readFile(e).then(e=>JSON.parse(e)).then(e=>r.length?e[Object.keys(e).find(e=>r.includes(e))]:e).catch(()=>({}))}function readOrWriteFile(e,r){return readFile(e).catch(()=>writeFile(e,r||"").then(()=>""))}function safelyReadFile(e){return readFile(e).catch(()=>"")}function writeFile(e,r){return new Promise((t,n)=>{fs.writeFile(e,r,e=>{e?n(e):t()})})}async function getOptions(){const e=process.cwd(),r={plugin:e,config:e,fixtures:path.resolve(e,"test")},t=await readJSON("package.json","postcss","postcssConfig").then(e=>getOptionsFromArguments(Object.assign(r,e)));try{t.plugin=await Promise.resolve(require(path.resolve(t.plugin)))}catch(e){throw e}try{t.config=await Promise.resolve(require(path.extname(t.config)?path.resolve(t.config):path.resolve(t.config,"postcss-tape.config.js")))}catch(e){try{t.config=await Promise.resolve(require(path.extname(t.config)?path.resolve(t.config):path.resolve(t.config,".tape.js")))}catch(e){throw e}}return t}const colors={reset:"",bold:"",dim:"",underline:"",blink:"",reverse:"",hidden:"",black:"",red:"",green:"",yellow:"",blue:"",magenta:"",cyan:"",white:"",bgBlack:"",bgRed:"",bgGreen:"",bgYellow:"",bgBlue:"",bgMagenta:"",bgCyan:"",bgWhite:""};function color(e,r){return colors[e]+r.replace(colors.reset,colors.reset+colors[e])+colors.reset}const isWin32="win32"===process.platform,tick=isWin32?"√":"✔",cross=isWin32?"×":"✖",stdout=process.stdout;let interval;function pass(e,r,t){clearInterval(interval),t?stdout.write(` ${color("green",tick)}\n`):(readline.clearLine(stdout,0),readline.cursorTo(stdout,0),stdout.write(`${color("green",tick)} ${e} ${color("dim",r)}\n`))}function fail(e,r,t,n){clearInterval(interval),n?stdout.write(` ${color("red",cross)}\n${t}\n`):(readline.clearLine(stdout,0),readline.cursorTo(stdout,0),stdout.write(`${color("red",cross)} ${e} ${color("dim",r)}\n${t}\n`))}function wait(e,r,t){if(t)stdout.write(`${e} ${color("dim",r)}`);else{const t=isWin32?"-–—–-":"⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏";let n=0;clearInterval(interval),readline.clearLine(stdout,0),readline.cursorTo(stdout,0),stdout.write(`${color("yellow",t[n])} ${e} ${color("dim",r)}`),interval=setInterval(()=>{n=(n+1)%t.length,readline.cursorTo(stdout,0),stdout.write(`${color("yellow",t[n])} ${e} ${color("dim",r)}`)},60)}}getOptions().then(async e=>{let r=!1;for(const t in e.config){const n=e.config[t],s=n.plugin instanceof Function?n.plugin:e.plugin,i=t.split(":")[0],o=t.split(":").join("."),a=path.resolve(e.fixtures,n.source||`${i}.css`),c=path.resolve(e.fixtures,n.expect||`${o}.expect.css`),l=path.resolve(e.fixtures,n.result||`${o}.result.css`),g=Object.assign({from:a,to:l},n.processOptions),u=n.options,p=Object(s.postcss).postcssPlugin||"postcss";wait(p,n.message,e.ci);try{Object(n.before)instanceof Function&&await n.before();const t=await safelyReadFile(c),i=await readOrWriteFile(a,t),o=await s.process(i,g,u),f=o.css;if(e.fix)await writeFile(c,f),await writeFile(l,f);else if(await writeFile(l,f),t!==f)throw new Error([`Expected: ${JSON.stringify(t).slice(1,-1)}`,`Received: ${JSON.stringify(f).slice(1,-1)}`].join("\n"));const d=o.warnings();if("number"==typeof n.warnings){if(n.warnings!==d.length){const e=1!==d.length?"s":"";throw new Error(`Expected: ${n.warnings} warning${e}\nReceived: ${d.length} warnings`)}}else if(d.length){if(!d.every(e=>n.warnings===Object(n.warnings)&&Object.keys(n.warnings).every(r=>n.warnings[r]instanceof RegExp?n.warnings[r].test(e[r]):n.warnings[r]===e[r]))){const e=1!==d.length?"s":"";throw new Error(`Unexpected warning${e}:\n${d.join("\n")}`)}}else{if(n.warnings)throw new Error("Expected a warning");if(n.errors)throw new Error("Expected an error")}Object(n.after)instanceof Function&&await n.after(),pass(p,n.message,e.ci)}catch(t){if(n.errors===Object(n.errors)&&Object.keys(n.errors).every(e=>n.errors[e]instanceof RegExp?n.errors[e].test(t[e]):n.error[e]===t[e]))pass(p,n.message,e.ci);else if(fail(p,n.message,t,e.ci),r=!0,e.ci)break}}if(r)throw new Error}).then(process.exit.bind(process,0),process.exit.bind(process,1));