export-helper
Advanced tools
Comparing version 0.1.1 to 1.0.0
@@ -1,6 +0,40 @@ | ||
const editCodeString = (string, isItIntoEs6) => { | ||
const output = isItIntoEs6 ? string.replace(/=/g, "default") : string.replace(/default/g, "="); | ||
const editCodeString = (string, mode) => { | ||
let output; | ||
const stuffToExport = string.replace( | ||
/\bexport\b|\bdefault\b|\bas\b|=|;|\s/g, | ||
"" | ||
); | ||
const severalThingsToExport = stuffToExport.includes(","); | ||
const prettyStuff = Array.from(stuffToExport) | ||
.map((char) => { | ||
if (char === "{" || char === ",") return `${char} `; | ||
else if (char === "}") return ` ${char}`; | ||
else return char; | ||
}) | ||
.join(""); | ||
const prettyStuffWithBrackets = (string) => | ||
string.includes("{") ? `${string};` : `{ ${prettyStuff} };`; | ||
if (mode === "es5") { | ||
output = `export = ${stuffToExport.replace(/{|}/g, "")};`; | ||
} | ||
if (mode === "es5:withbrackets") { | ||
output = `export = ${prettyStuffWithBrackets(prettyStuff)}`; | ||
} | ||
if (mode === "es6" || mode === "es6:named") { | ||
output = `export ${prettyStuffWithBrackets(prettyStuff)}`; | ||
} | ||
if (mode === "es6:default") { | ||
output = `export default ${prettyStuffWithBrackets(prettyStuff)}`; | ||
} | ||
if (mode === "es6:asDefault") { | ||
output = severalThingsToExport`export ${prettyStuff.replace( | ||
"}", | ||
"as default }" | ||
)};`; | ||
} | ||
return output; | ||
} | ||
}; | ||
module.exports = editCodeString; | ||
module.exports = editCodeString; |
60
index.js
@@ -1,2 +0,2 @@ | ||
const readLastLines = require('read-last-lines'); | ||
const readLastLines = require("read-last-lines"); | ||
const trim = require("./trim"); | ||
@@ -23,27 +23,39 @@ const append = require("./append"); | ||
return readLastLines.read(path, linesToTrim) | ||
.then(async (res) => { | ||
return readLastLines.read(path, linesToTrim).then(async (res) => { | ||
const log = (oldString, newString) => | ||
console.log( | ||
`npmjs/export-helper: "${oldString.replace( | ||
"\n", | ||
"" | ||
)}" has successfully been replaced by "${newString.replace( | ||
"\n", | ||
"" | ||
)}" (${path})` | ||
); | ||
if (mode === "es5") { | ||
await trim(path, linesToTrim); | ||
const newString = await editCodeString(res, false); | ||
await append(path, await newString); | ||
if (!silent) { | ||
console.log(`npmjs/export-helper: "${res}" has successfully be replaced by "${newString}" (${path})`) | ||
} | ||
return null; | ||
} | ||
let newString; | ||
await trim(path, linesToTrim); | ||
if (mode === "es6") { | ||
await trim(path, linesToTrim); | ||
const newString = await editCodeString(res, true); | ||
await append(path, await newString); | ||
if (!silent) { | ||
console.log(`npmjs/export-helper: "${res}" has successfully be replaced by "${newString}" (${path})`) | ||
} | ||
return null; | ||
} | ||
}) | ||
} | ||
if (mode === "es5") { | ||
newString = await editCodeString(res, "es5"); | ||
} else if (mode === "es5:withbrackets") { | ||
newString = await editCodeString(res, "es5:withbrackets"); | ||
} else if (mode === "es6" || mode === "es6:named") { | ||
newString = await editCodeString(res, "es6:named"); | ||
} else if (mode === "es6:default") { | ||
newString = await editCodeString(res, "es6:default"); | ||
} else if (mode === "es6:asDefault") { | ||
newString = await editCodeString(res, "es6:asDefault"); | ||
} else { | ||
throw new Error( | ||
"'mode' value must be one of: es5, es6, es6:default, es6:named, es6:asDefault" | ||
); | ||
} | ||
module.exports = exportHelper; | ||
await append(path, await newString); | ||
if (!silent) log(res, newString); | ||
return null; | ||
}); | ||
}; | ||
module.exports = exportHelper; |
{ | ||
"name": "export-helper", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "Nodejs utility to help compile TS modules into sweet es5 & es6 exports", | ||
@@ -9,2 +9,5 @@ "main": "index.js", | ||
}, | ||
"bin": { | ||
"export-helper": "cli.js" | ||
}, | ||
"keywords": [ | ||
@@ -11,0 +14,0 @@ "export-helper", |
@@ -1,7 +0,10 @@ | ||
# export-helper | ||
# export-helper | ||
Pretentious name for a tiny **nodejs** module that edits the last line of a file. | ||
### Why ? | ||
It basically turns `export = myModule;` into `default export myModule;` between tsc compilations (both ways supported) | ||
Pretentious name for a tiny **nodejs** module that edits the last line of a file. | ||
### Why ? | ||
It basically turns `export = myModule;` into `export { myModule };` between tsc compilations (both ways supported) | ||
Use it to compile into the syntax of Old (`require()`, NOT `require().default`) and a valid es6 module, in a single build command. | ||
[![https://nodei.co/npm/export-helper.png?downloads=true&downloadRank=true&stars=true](https://nodei.co/npm/export-helper.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/export-helper) | ||
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) | ||
@@ -11,25 +14,48 @@ [![Known Vulnerabilities](https://snyk.io/test/github/TheRealBarenziah/export-helper/badge.svg?targetFile=package.json)](https://snyk.io/test/github/TheRealBarenziah/export-helper?targetFile=package.json) | ||
### Compatibility | ||
**node >= 10** | ||
### Install | ||
`npm install --save-dev export-helper` | ||
**node >= 10** | ||
### Use | ||
Javascript file `helper.js`: | ||
### Install | ||
`npm install --save-dev export-helper` | ||
### Use (CLI) | ||
```bash | ||
npx export-helper es5 index.ts | ||
``` | ||
- first argument can be either "es5" or "es6" | ||
- second argument is target file path | ||
- accepts an optional 'verbose' 3rd param, that enables a log: | ||
```bash | ||
npx export-helper es6 testFile.ts verbose | ||
# npmjs/export-helper: "export = constant;" has successfully been replaced by "export { constant };" (testFile.ts) | ||
``` | ||
### Use (from JS) | ||
Javascript file `helper.js`: | ||
```javascript | ||
const exportHelper = require("export-helper"); | ||
exportHelper({ mode: "es6", path: "testFile.ts" }) | ||
.then(res => res); | ||
``` | ||
In terminal: | ||
exportHelper({ mode: "es6", path: "testFile.ts" }).then((res) => res); | ||
``` | ||
In terminal: | ||
```bash | ||
$ node helper.js | ||
$ npmjs/export-helper: "export = constant;" has successfully be replaced by "export default constant;" (testFile.ts) | ||
``` | ||
$ npmjs/export-helper: "export = constant;" has successfully been replaced by "export { constant };" (testFile.ts) | ||
``` | ||
### Options | ||
This function accepts an option object : | ||
This function accepts an option object : | ||
```javascript | ||
const options = { | ||
mode: "es6", // needed. Available options: "es5", "es6" | ||
const options = { | ||
mode: "es6", // needed. Available options: "es5", "es6". Untested options: "es5:withbrackets", "es6:default", "es6asDefault" | ||
path: "testFile.ts", // needed. Path to file (./ is optional) | ||
@@ -41,34 +67,20 @@ silent: false, // default to false; set to true to remove the log | ||
incrementing that value should do the trick. | ||
*/ | ||
*/, | ||
}; | ||
``` | ||
``` | ||
### ... more examples ? | ||
This is the configuration on the [module](https://github.com/TheRealBarenziah/imgbb-uploader/blob/master) I made this for. | ||
I have an utility file updateExport.js : | ||
```javascript | ||
const exportHelper = require("export-helper"); | ||
const updateExport = () => { | ||
if (process.argv.slice(2)[0] === "es5") { | ||
exportHelper({ mode: "es5", path: "src/index.ts" }).then((res) => res); | ||
} else if (process.argv.slice(2)[0] === "es6") { | ||
exportHelper({ mode: "es6", path: "src/index.ts" }).then((res) => res); | ||
} else | ||
throw "Oopsie, it seems you forgot to pass either 'es5' or 'es6' as argument !"; | ||
}; | ||
This script in my package.json : | ||
updateExport(); | ||
``` | ||
And this script in my package.json : | ||
```json | ||
{ | ||
{ | ||
... | ||
"npm run build": "node rebuild.js && node updateExport.js es5 && tsc -p tsconfig-cjs.json && node updateExport.js es6 && tsc -p tsconfig.json" | ||
"npm run build": "node rebuild.js && npx export-helper es5 src/index.ts && tsc -p tsconfig-cjs.json && px export-helper es6 src/index.ts && tsc -p tsconfig.json" | ||
} | ||
``` | ||
- rebuild.js is a [simple utility](https://github.com/TheRealBarenziah/imgbb-uploader/blob/dev/rebuild.js) that wipes the /lib folder before compilation. | ||
- `node updateExport.js es5` : I wrapped my module into a function using `process.argv` to easily pass arguments to it. | ||
- `tsc -p tsconfig-cjs.json`: I'm calling tsc with [this config file](https://github.com/TheRealBarenziah/imgbb-uploader/blob/dev/tsconfig-cjs.json) | ||
- Now that I've compiled proper cjs with `module.exports`, I call `node updateExport.js es6` to change the source code once more. | ||
- Finally I call tsc to build the es6 module. | ||
``` | ||
- rebuild.js is a [simple utility](https://github.com/TheRealBarenziah/imgbb-uploader/blob/dev/rebuild.js) that wipes the /lib folder before compilation. | ||
- `tsc -p tsconfig-cjs.json`: I'm calling tsc with [this config file](https://github.com/TheRealBarenziah/imgbb-uploader/blob/dev/tsconfig-cjs.json) | ||
- Now that I've compiled proper cjs with `module.exports`, I call `npx export-helper es6 src/index.ts ` to change the source code once more. | ||
- Finally I call tsc to build the es6 module. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9745
8
140
0
85