Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix up the package's import path #7

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
out
npm-debug.log
/coverage
/test/fixture/esdoc
/test/fixture/esdoc*
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
language: node_js
sudo: false
node_js:
- "0.12"
- "6.3.1"

script:
- TRAVIS=1 npm run test-es5
- npm run build
- npm run coverage:travis

install:
- npm install
80 changes: 67 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Therefore, convert the import path by using following setting.
{
"name": "esdoc-importpath-plugin",
"option": {
// appendPackageName and replaces will be ignored when setting the packageProp
"packageProp": "main",
"appendPackageName": true,
"replaces": [
{"from": "^src/", "to": "lib/"}
]
Expand All @@ -30,21 +33,49 @@ Therefore, convert the import path by using following setting.
}
```

``from`` is regular expression and ``to``is letter. In the internal ``from`` and ``to`` are used with ``String#replace(new RegExp (from), to)``.
### packageProp [string]

Resolves property inside the `package.json' file. Generally set to either `main` or `name`. Anytime packageProp is set, the `option.replaces` transformation will be ignored.

**note:** Prefer setting your project's `package.json` main property instead of using this override to better adhere to [npm docs's main spec](https://docs.npmjs.com/files/package.json#main).

### appendPackageName [boolean]

Appends the package name to the import path.

### replaces [array] or [string]

When writing multi rules, it will also be carried out transformation many times.
For example, ``[{from: "^src/", to: "lib/"}, {from: "MyFooClass", to: "my-foo"}]`` converted as follows:
Replaces can be either an array or string.

- `` my-module/src/MyFooClass.js`` => `` my-module/lib/MyFooClass.js`` => ``my-module/lib/my-foo``
#### replaces [string]

# Install and Usage
```sh
npm install esdoc-importpath-plugin
If replaces is a string, then the import path will always be that defined string.
```json
// esdocs.json
{
"source": "./src",
"destination": "./doc",
"plugins": [
{
"name": "esdoc-importpath-plugin",
"option": {
"replaces": "my-import-override",
}
}
]
}
```

setup ``plugin`` property in ``esdoc.json``
#### replaces [array]
Otherwise, if replaces is an array, then each item in the replaces must be an object consisting of `from` and `to` properties.

``from`` is regular expression and ``to``is letter. In the internal ``from`` and ``to`` are used with ``String#replace(new RegExp (from), to)``.

When writing multi rules, the `replaces` transformations will be executed in the exact order that is defined in the array.

For example, ``my-module/src/MyFooClass.js`` => `` my-module/lib/MyFooClass.js`` => ``my-module/lib/my-foo`` with the following config:
```json
// esdocs.json
{
"source": "./src",
"destination": "./doc",
Expand All @@ -53,19 +84,42 @@ setup ``plugin`` property in ``esdoc.json``
"name": "esdoc-importpath-plugin",
"option": {
"replaces": [
{"from": "^src/", "to": "lib"}
{ from: "^src/", to: "lib/" },
{ from: "MyFooClass", to: "my-foo" }
]
}
}
]
}
```

execute ESDoc
# Setup

```json
esdoc -c esdoc.json
```
1. Install `esdoc-importpath-plugin`.
```sh
$ npm install esdoc-importpath-plugin --save-dev
```
1. setup ``plugin`` property in ``esdoc.json``
```json
{
"source": "./src",
"destination": "./doc",
"plugins": [
{
"name": "esdoc-importpath-plugin",
"option": {
"replaces": [
{"from": "^src/", "to": "lib"}
]
}
}
]
}
```
1. Execute ESDoc
```sh
$ esdoc -c esdoc.json
```

# LICENSE
MIT
Expand Down
18 changes: 0 additions & 18 deletions esdoc.json

This file was deleted.

15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
"url": "https://github.com/esdoc/esdoc-importpath-plugin"
},
"scripts": {
"build": "node ./script/build.js",
"test": "node ./script/test.js",
"test-es5": "node ./script/test-es5.js",
"build": "rm -rf out && mkdir -p out/src && babel -d out/src src",
"test": "rm -rf ./test/fixture/esdoc* && mocha -t 10000 -R spec --compilers js:babel-core/register --recursive ./test/src",
"coverage": "istanbul cover _mocha --report lcovonly -- -t 10000 --compilers js:babel-core/register --recursive ./test/src -R spec",
"coverage:travis": "npm run coverage -s && cat ./coverage/lcov.info | coveralls",
"esdoc": "./node_modules/.bin/esdoc -c esdoc.json"
},
"devDependencies": {
"babel": "^5.8.20",
"babel-cli": "^6.9.0",
"babel-core": "^6.13.2",
"babel-eslint": "^6.1.0",
"babel-preset-es2015": "^6.1.18",
"coveralls": "~2.11.2",
"esdoc": "^0.2.0",
"espower-babel": "^3.2.0",
"esdoc": "^0.4.8",
"fs-extra": "^0.22.1",
"istanbul": "~0.3.16",
"mocha": "~2.2.5",
Expand Down
11 changes: 0 additions & 11 deletions script/build.js

This file was deleted.

35 changes: 0 additions & 35 deletions script/sh.js

This file was deleted.

14 changes: 0 additions & 14 deletions script/test-es5.js

This file was deleted.

12 changes: 0 additions & 12 deletions script/test.js

This file was deleted.

74 changes: 46 additions & 28 deletions src/Plugin.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import fs from 'fs';

let option;
let packagePath = './package.json';
let packageObj;

/**
* take option
* @param {Object} ev - handle event.
*/
export function onStart(ev) {
option = ev.data.option;
for (let item of option.replaces) {
item.from = new RegExp(item.from);

if (Array.isArray(option.replaces)) {
option.appendPackageName = option.appendPackageName;
option.replaces = option.replaces.map((item) => {
return {
to: item.to,
from: new RegExp(item.from),
};
});
} else if (typeof option.replaces === 'string') {
option.name = option.replaces;
}
}

Expand All @@ -19,7 +28,18 @@ export function onStart(ev) {
* @param {Object} ev - handle event.
*/
export function onHandleConfig(ev) {
if (ev.data.config.package) packagePath = ev.data.config.package;
packageObj = ev.data.config.package;
if (typeof ev.data.config.package === 'string') {
try {
const packageJSON = fs.readFileSync(packageObj).toString();
packageObj = JSON.parse(packageJSON);
} catch (e) {
// ignore
}
}
if (typeof option.packageProp === 'string' && packageObj) {
option.name = packageObj[option.packageProp];
}
}

/**
Expand All @@ -28,33 +48,31 @@ export function onHandleConfig(ev) {
*/
export function onHandleTag(ev) {
// get package.json
let packageName = '';
let mainPath = '';
try {
const packageJSON = fs.readFileSync(packagePath).toString();
const packageObj = JSON.parse(packageJSON);
packageName = packageObj.name;
if(packageObj.main) mainPath = packageObj.main;
} catch (e) {
// ignore
}

for (let tag of ev.data.tag) {
if (!tag.importPath) continue;
if (tag.importPath) {
tag.importPath = getImportPath(tag.importPath);
}
}
}

let importPath = tag.importPath;
if (packageName) importPath = importPath.replace(new RegExp(`^${packageName}/`), '');
function getImportPath(tagImportPath) {
if (option.name) {
return option.name;
}

for (let item of option.replaces) {
importPath = importPath.replace(item.from, item.to);
}
// process the user's replace config.
let importPath = tagImportPath;
if (packageObj && packageObj.name) {
importPath = importPath.replace(new RegExp(`^${packageObj.name}/`), '');
}
(option.replaces || []).forEach((item) => {
importPath = importPath.replace(item.from, item.to);
});

if (importPath === mainPath) {
tag.importPath = packageName;
} else if (packageName) {
tag.importPath = `${packageName}/${importPath}`;
} else {
tag.importPath = importPath;
}
// add the package name to the beginning of the import path
if (option.appendPackageName) {
return `${packageObj.name}/${importPath}`;
}

return importPath;
}
4 changes: 0 additions & 4 deletions test/espower-babel-loader.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/fixture/esdoc.json

This file was deleted.

4 changes: 0 additions & 4 deletions test/fixture/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixture/src/MyClass.js

This file was deleted.

Loading