Skip to content

Commit 766fc19

Browse files
motiz88facebook-github-bot
authored andcommitted
feat: create npm package (#74)
Summary: Closes #50. - [x] Basic structure of the package - [x] Publish under a temporary name ([`motizilberman/dotslash`](https://www.npmjs.com/package/motizilberman/dotslash)) - Done using `npm run build -- --version 0.5.7 --prerelease` - [x] Test on all platforms - [x] Iterate on README and test the workflows described there - [x] Add docs - [x] Set up GitHub Action for publishing to npm on every release - [x] Add Flow and TypeScript definition files for convenience - [x] Decide on a final package name and publish a non-prerelease version - ~We can either ask npm nicely to free up `dotslash` - currently unavailable because of [`dot-slash`](https://www.npmjs.com/package/dot-slash), a package last published 10 years ago with 5 weekly downloads - or go with e.g. `fb-dotslash` (which I have [reserved](https://www.npmjs.com/package/fb-dotslash)).~ - --> `fb-dotslash` - [x] Transfer package to Meta - --> added `fb` as a maintainer Possible follow-up scope (definitely not in this PR): - Add an *optional* postinstall script that replaces `bin/dotslash` with a symlink to the correct binary, thus making DotSlash'd tools start even faster in environments that respect postinstall scripts. ## How to publish the package ``` cd node npm ci npm run lint npm run build -- --version $RELEASED_DOTSLASH_VERSION # add --prerelease for testing npm publish ``` Differential Revision: D79818880 Pulled By: motiz88
1 parent d23a39f commit 766fc19

15 files changed

Lines changed: 535 additions & 0 deletions

File tree

.github/workflows/release.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,24 @@ jobs:
242242
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
243243
run: gh release upload %GITHUB_REF:~10% dotslash-windows-arm64.%GITHUB_REF:~10%.tar.gz
244244
shell: cmd
245+
246+
npm-publish:
247+
# This job depends on release assets uploaded by the previous jobs.
248+
# Keep this job's dependencies in sync with node/platforms.js.
249+
needs: [macos, windows, windows-arm64, linux-musl-x86_64, linux-musl-arm64]
250+
runs-on: ubuntu-latest
251+
defaults:
252+
run:
253+
working-directory: node
254+
steps:
255+
- uses: actions/checkout@v4
256+
# Setup .npmrc file to publish to npm
257+
- uses: actions/setup-node@v4
258+
with:
259+
node-version: '20.x'
260+
registry-url: 'https://registry.npmjs.org'
261+
- run: npm ci
262+
- run: npm run build -- --tag ${GITHUB_REF#refs/tags/}
263+
- run: npm publish --provenance --access public
264+
env:
265+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/test-node.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: JavaScript chores
2+
3+
on: [push, pull_request, workflow_dispatch]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
defaults:
12+
run:
13+
working-directory: node
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-node@v4
17+
with:
18+
node-version: '20.x'
19+
registry-url: 'https://registry.npmjs.org'
20+
- run: npm ci
21+
- run: npm run lint

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.DS_Store
22
/target
3+
**/node_modules
4+
node/bin/*/

node/.prettierrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"singleQuote": true,
3+
"overrides": [
4+
{
5+
"files": "bin/dotslash",
6+
"options": { "parser": "babel" }
7+
}
8+
]
9+
}

node/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# DotSlash: simplified executable deployment
2+
3+
[DotSlash](https://dotslash-cli.com/docs/) (`dotslash`) is a command-line tool that lets you represent a set of
4+
platform-specific, heavyweight executables with an equivalent small,
5+
easy-to-read text file. In turn, this makes it efficient to store executables in
6+
source control without hurting repository size. This paves the way for checking
7+
build toolchains and other tools directly into the repo, reducing dependencies
8+
on the host environment and thereby facilitating reproducible builds.
9+
10+
The `fb-dotslash` npm package allows you to use DotSlash in your Node.js projects without having to install DotSlash globally. This is particularly useful for package authors, who have traditionally needed to either include binaries for _all_ platforms or manage their own download and caching in a postinstall script.
11+
12+
## Using DotSlash in an npm package
13+
14+
First, you'll need to write a [DotSlash file](https://dotslash-cli.com/docs/dotslash-file/) that describes the binary you want to distribute.
15+
16+
If your npm package declares `fb-dotslash` as a dependency, any commands executed as part of `npm run` and `npm exec` will have `dotslash` available on the `PATH`. This means you can, for example, directly reference DotSlash files in your `package.json` scripts with no further setup:
17+
18+
```json
19+
{
20+
"name": "my-package",
21+
"scripts": {
22+
"foo": "path/to/dotslash/file"
23+
},
24+
"dependencies": {
25+
"fb-dotslash": "^0.5.7"
26+
}
27+
}
28+
```
29+
30+
If you need to use `dotslash` in some other context, you can use `require('fb-dotslash')` to get the path to the DotSlash executable appropriate for the current platform:
31+
32+
```js
33+
const dotslash = require('fb-dotslash');
34+
const {spawnSync} = require('child_process');
35+
spawnSync(dotslash, ['path/to/dotslash/file'], {stdio: 'inherit']);
36+
```
37+
38+
## License
39+
40+
DotSlash is licensed under both the MIT license and Apache-2.0 license; the
41+
exact terms can be found in the [LICENSE-MIT](https://github.com/facebook/dotslash/blob/main/LICENSE-MIT) and
42+
[LICENSE-APACHE](https://github.com/facebook/dotslash/blob/main/LICENSE-APACHE) files, respectively.

node/bin/dotslash

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
/*
3+
* Copyright (c) Meta Platforms, Inc. and affiliates.
4+
*
5+
* This source code is licensed under both the MIT license found in the
6+
* LICENSE-MIT file in the root directory of this source tree and the Apache
7+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
8+
* of this source tree.
9+
*/
10+
11+
'use strict';
12+
13+
const spawn = require('child_process').spawn;
14+
15+
const input = process.argv.slice(2);
16+
const bin = require('../');
17+
18+
if (bin !== null) {
19+
spawn(bin, input, { stdio: 'inherit' }).on('exit', process.exit);
20+
} else {
21+
throw new Error('Platform not supported.');
22+
}

node/index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is dual-licensed under either the MIT license found in the
5+
* LICENSE-MIT file in the root directory of this source tree or the Apache
6+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7+
* of this source tree. You may select, at your option, one of the
8+
* above-listed licenses.
9+
*
10+
* @format
11+
*/
12+
13+
declare const binaryPath: string;
14+
export = binaryPath;

node/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is dual-licensed under either the MIT license found in the
5+
* LICENSE-MIT file in the root directory of this source tree or the Apache
6+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7+
* of this source tree. You may select, at your option, one of the
8+
* above-listed licenses.
9+
*/
10+
11+
'use strict';
12+
13+
const os = require('os');
14+
const path = require('path');
15+
const { artifactsByPlatformAndArch } = require('./platforms');
16+
17+
const artifacts = artifactsByPlatformAndArch[os.platform()];
18+
const { slug, binary } = artifacts[os.arch()] ?? artifacts['*'];
19+
20+
module.exports = path.join(__dirname, 'bin', slug, binary);

node/index.js.flow

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is dual-licensed under either the MIT license found in the
5+
* LICENSE-MIT file in the root directory of this source tree or the Apache
6+
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7+
* of this source tree. You may select, at your option, one of the
8+
* above-listed licenses.
9+
*
10+
* @format
11+
* @flow strict-local
12+
*/
13+
14+
declare var binaryPath: string;
15+
module.exports = binaryPath;

node/package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)