Skip to content

Commit a07b407

Browse files
committed
[added] alternative package root folder option
1 parent fa9fd7c commit a07b407

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ including `bower` publishing, for you - automatically.
3535
_Initial idea is got from `React-Bootstrap` release tools `./tools/release`,
3636
that have been written by [Matt Smith @mtscout6](https://github.com/mtscout6)_
3737

38+
#### Alternative npm package root folder
39+
40+
Say you want to publish to `npmjs` only the content of your `lib` folder.
41+
Then you can do it as simple as adding this option to your `package.json`
42+
```json
43+
"release-script": {
44+
"altPkgRootFolder": "lib"
45+
}
46+
```
47+
and that's all.
48+
3849
#### Options
3950

4051
All options for this package are kept under `'release-script'` node in your project's `package.json`
@@ -44,6 +55,7 @@ All options for this package are kept under `'release-script'` node in your proj
4455
- `default` value: `'amd'`
4556
- `tmpBowerRepo` - the folder name for temporary files for bower pkg.
4657
- `default` value: `'tmp-bower-repo'`
58+
- `altPkgRootFolder` - the folder name for alternative npm package root folder
4759

4860
It is advised to add `bowerRoot` and `tmpBowerRepo` folders to your `.gitignore` file.
4961

@@ -54,7 +66,8 @@ E.g.:
5466
"release-script": {
5567
"bowerRepo": "[email protected]:<org-author-name>/<name-of-project>-bower.git",
5668
"bowerRoot": "amd",
57-
"tmpBowerRepo": "tmp-bower-repo"
69+
"tmpBowerRepo": "tmp-bower-repo",
70+
"altPkgRootFolder": "lib"
5871
}
5972
```
6073

@@ -89,7 +102,11 @@ You can set a custom message for release via `--notes` CLI option:
89102
- adds git tag with new version (and changelog message, if used)
90103
- pushes changes to github repo
91104
- if github token is present, publishes release to GitHub, named as `<repo> vx.x.x`
92-
- releases npm package by `npm publish` command
105+
- if `altPkgRootFolder` doesn't set it will just `npm publish` as usual
106+
- otherwise if `altPkgRootFolder` set then this script
107+
- will `npm publish` from the `altPkgRootFolder` folder
108+
- with the custom version of `package.json` with removed `scripts` and `devDependencies`
109+
- also it will remove the `altPkgRootFolder` part from the `main` file path
93110
- if `bowerRepo` field is present in the `package.json`, then it releases bower package:
94111
- clones bower repo to local `tmpBowerRepo` temp folder. `git clone bowerRepo tmpBowerRepo`
95112
- then it cleans up all but `.git` files in the `tmpBowerRepo`

src/release.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const bowerRepo = configOptions.bowerRepo; // if it is not set, then there is no
3838

3939
const githubToken = process.env.GITHUB_TOKEN;
4040

41+
const altPkgRootFolder = configOptions.altPkgRootFolder;
4142

4243
//------------------------------------------------------------------------------
4344
// command line options
@@ -256,7 +257,26 @@ function release({ type, preid }) {
256257
console.log('Package is private, skipping npm release'.yellow);
257258
} else {
258259
console.log('Releasing: '.cyan + 'npm package'.green);
259-
safeRun('npm publish');
260+
261+
// publishing just /altPkgRootFolder content
262+
if (altPkgRootFolder) {
263+
// prepare custom `package.json` without `scripts` and `devDependencies`
264+
// because it already has been saved, we safely can use the same object
265+
delete npmjson.files; // because otherwise it would be wrong
266+
delete npmjson.scripts;
267+
delete npmjson.devDependencies;
268+
delete npmjson['release-script']; // this also doesn't belong to output
269+
const regexp = new RegExp(altPkgRootFolder + '\\/?');
270+
npmjson.main = npmjson.main.replace(regexp, ''); // remove folder part from path
271+
`${JSON.stringify(npmjson, null, 2)}\n`.to(path.join(altPkgRootFolder, 'package.json'));
272+
273+
pushd(altPkgRootFolder);
274+
safeRun('npm publish');
275+
popd();
276+
} else {
277+
safeRun('npm publish');
278+
}
279+
260280
console.log('Released: '.cyan + 'npm package'.green);
261281
}
262282

0 commit comments

Comments
 (0)