Skip to content

Commit da589fc

Browse files
authored
Add CI and auto publishing (#3)
1 parent 326d52c commit da589fc

File tree

13 files changed

+253
-22
lines changed

13 files changed

+253
-22
lines changed

.github/auto-publish-action.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const path = require('path');
2+
const { EOL } = require('os');
3+
const { existsSync } = require('fs');
4+
const { spawn } = require('child_process');
5+
6+
const workspace = process.env.GITHUB_WORKSPACE;
7+
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
8+
9+
(async () => {
10+
const event = process.env.GITHUB_EVENT_PATH ? require(process.env.GITHUB_EVENT_PATH) : {};
11+
12+
console.log('release event action:', event.action);
13+
14+
if (event.action !== 'published' || !event.release) {
15+
exitSuccess('This action is only run when a release is published.');
16+
return;
17+
}
18+
19+
console.log('release tag name:', event.release.tag_name);
20+
21+
if (!event.release.tag_name) {
22+
exitSuccess('This action is only run when a release is published with a tag name.');
23+
return;
24+
}
25+
26+
const releaseTargetBranch = event.release.target_commitish;
27+
28+
console.log('release target branch:', releaseTargetBranch);
29+
30+
if (releaseTargetBranch !== 'main') {
31+
exitSuccess('This action is only run when a release is published from the main branch.');
32+
return;
33+
}
34+
35+
const releaseVersion = event.release.tag_name.startsWith('v')
36+
? event.release.tag_name.slice(1)
37+
: event.release.tag_name;
38+
39+
console.log('release version:', releaseVersion);
40+
41+
const pkg = getPackageJson();
42+
const packageVersion = pkg.version.toString();
43+
44+
console.log('package.json version:', packageVersion);
45+
46+
if (packageVersion !== releaseVersion) {
47+
exitFailure('The version in package.json and the release tag version do not match.');
48+
return;
49+
}
50+
51+
try {
52+
console.log('publishing package...');
53+
54+
await runInWorkspace(npmCommand, ['publish']);
55+
56+
exitSuccess(`Package version v${releaseVersion} published!`);
57+
} catch (e) {
58+
logError(e);
59+
exitFailure('Failed to publish package');
60+
return;
61+
}
62+
})();
63+
64+
function getPackageJson() {
65+
const pathToPackage = path.join(workspace, 'package.json');
66+
if (!existsSync(pathToPackage))
67+
throw new Error("package.json could not be found in your project's root.");
68+
return require(pathToPackage);
69+
}
70+
71+
function exitSuccess(message) {
72+
console.info(`✔ success ${message}`);
73+
process.exit(0);
74+
}
75+
76+
function exitFailure(message) {
77+
logError(message);
78+
process.exit(1);
79+
}
80+
81+
function logError(error) {
82+
console.error(`✖ fatal ${error.stack || error}`);
83+
}
84+
85+
function runInWorkspace(command, args) {
86+
return new Promise((resolve, reject) => {
87+
const child = spawn(command, args, { cwd: workspace });
88+
89+
const errorMessages = [];
90+
let isDone = false;
91+
92+
child.on('error', (error) => {
93+
if (!isDone) {
94+
isDone = true;
95+
reject(error);
96+
}
97+
});
98+
99+
child.stderr.on('data', (chunk) => errorMessages.push(chunk));
100+
101+
child.on('exit', (code) => {
102+
if (!isDone) {
103+
if (code === 0) {
104+
resolve();
105+
} else {
106+
reject(`${errorMessages.join('')}${EOL}${command} exited with code ${code}`);
107+
}
108+
}
109+
});
110+
});
111+
}

.github/workflows/formatting.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: JS and C++ Formatting
5+
6+
on:
7+
push:
8+
branches: [main]
9+
pull_request:
10+
branches: [main]
11+
12+
jobs:
13+
js-formatting:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- name: Use Node.js 16
19+
uses: actions/setup-node@v2
20+
with:
21+
node-version: '16.x'
22+
cache: 'yarn'
23+
24+
- run: yarn install
25+
- run: yarn format:check
26+
27+
cpp-formatting:
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v2
31+
32+
- name: Check C++ formatting with clang-format
33+
uses: DoozyX/clang-format-lint-action@v0.13
34+
with:
35+
source: './src'
36+
exclude: './src/mio ./src/xxhash ./src/zstr'
37+
extensions: 'h,c,cpp,hpp'
38+
clangFormatVersion: 12

.github/workflows/node.js.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [main]
9+
pull_request:
10+
branches: [main]
11+
12+
jobs:
13+
build-and-test:
14+
runs-on: ${{ matrix.os }}
15+
16+
strategy:
17+
matrix:
18+
os: [ubuntu-latest, windows-2019, macos-latest]
19+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
20+
node-version: [12.x, 14.x, 16.x]
21+
22+
steps:
23+
- uses: actions/checkout@v2
24+
25+
- name: Use Node.js ${{ matrix.node-version }}
26+
uses: actions/setup-node@v2
27+
with:
28+
node-version: ${{ matrix.node-version }}
29+
cache: 'yarn'
30+
31+
- run: yarn install
32+
- run: yarn test
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Publish to npm
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
publish_to_npm:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
with:
13+
ref: main
14+
15+
# Installs Node and sets up up the .npmrc file to publish to npm
16+
- uses: actions/setup-node@v2
17+
with:
18+
node-version: '16.x'
19+
registry-url: 'https://registry.npmjs.org'
20+
cache: 'yarn'
21+
22+
- run: yarn install
23+
- run: yarn test
24+
- run: node .github/auto-publish-action.js
25+
env:
26+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode
22
build
33
node_modules
4-
yarn-error.log
4+
yarn-error.log
5+
_deleted

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@ The following example shows a typical usage pattern of creating a decoder with n
2020
const { NbsDecoder } = require('nbsjs');
2121

2222
// Create a decoder instance
23-
const decoder = new NbsDecoder(['/path/to/file/a.nbs', '/path/to/file/b.nbs', '/path/to/file/c.nbs']);
23+
const decoder = new NbsDecoder([
24+
'/path/to/file/a.nbs',
25+
'/path/to/file/b.nbs',
26+
'/path/to/file/c.nbs',
27+
]);
2428

2529
// Get a list of all types present in the NBS files, and get the first type
2630
const types = decoder.getAvailableTypes();
27-
const firstType = types[0]
31+
const firstType = types[0];
2832

2933
// Set the timestamp range for the first available type
3034
const [start, end] = decoder.getTimestampRange(firstType);
3135

3236
// Get the packet at the given timestamp for the first type
33-
const packets = decoder.getPackets(start, [firstType])
37+
const packets = decoder.getPackets(start, [firstType]);
3438

3539
// Log all the values for inspection
36-
console.log({ types, firstType, start, end, packets })
40+
console.log({ types, firstType, start, end, packets });
3741
```
3842

3943
## API

binding.gyp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,33 @@
2424
'OS=="linux"',
2525
{
2626
"ccflags": [
27-
"-std=c++17",
27+
"-std=c++14",
2828
"-fPIC",
2929
"-fext-numeric-literals",
3030
"-fexceptions",
3131
],
3232
"ccflags!": ["-fno-exceptions"],
33-
"cflags_cc": ["-std=c++17", "-fext-numeric-literals"],
33+
"cflags_cc": ["-std=c++14", "-fext-numeric-literals"],
3434
"cflags_cc!": ["-fno-exceptions", "-fno-rtti"],
3535
},
3636
],
3737
[
3838
'OS=="mac"',
3939
{
4040
"ccflags": [
41-
"-std=c++17",
41+
"-std=c++14",
4242
"-fPIC",
4343
"-fext-numeric-literals",
4444
"-fexceptions",
4545
],
4646
"ccflags!": ["-fno-exceptions"],
47-
"cflags_cc": ["-std=c++17", "-fext-numeric-literals"],
47+
"cflags_cc": ["-std=c++14", "-fext-numeric-literals"],
4848
"cflags_cc!": ["-fno-exceptions", "-fno-rtti"],
4949
"xcode_settings": {
5050
"MACOSX_DEPLOYMENT_TARGET": "10.9",
5151
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
5252
"GCC_ENABLE_CPP_RTTI": "YES",
53-
"OTHER_CPLUSPLUSFLAGS": ["-std=c++17", "-stdlib=libc++"],
53+
"OTHER_CPLUSPLUSFLAGS": ["-std=c++14", "-stdlib=libc++"],
5454
"OTHER_LDFLAGS": ["-stdlib=libc++"],
5555
},
5656
},
@@ -61,7 +61,7 @@
6161
"defines": ["_HAS_EXCEPTIONS=1"],
6262
"msvs_settings": {
6363
"VCCLCompilerTool": {
64-
"AdditionalOptions": ["-std:c++17"],
64+
"AdditionalOptions": ["-std:c++14"],
6565
},
6666
},
6767
},

nbs.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export interface NbsTypeSubtype {
4040
* A (type, subtype) pair that uniquely identifies a specific type of message,
4141
* where the type is always a Buffer
4242
*/
43-
export interface NbsTypeSubtypeBuffer extends NbsTypeSubtype {
44-
type: Buffer
43+
export interface NbsTypeSubtypeBuffer extends NbsTypeSubtype {
44+
type: Buffer;
4545
}
4646

4747
/**

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"scripts": {
1111
"build": "node-gyp configure && node-gyp build",
1212
"test": "node tests/test.js",
13-
"format": "prettier --write *.js *.d.ts .github/**/*.js tests/*.js",
14-
"format:check": "prettier --check *.js *.d.ts .github/**/*.js tests/*.js"
13+
"format": "prettier --write \"*.{js,ts,json,md}\" \".github/**/*.{js,yml}\" \"tests/*.js\"",
14+
"format:check": "prettier --check \"*.{js,ts,json,md}\" \".github/**/*.{js,yml}\" \"tests/*.js\""
1515
},
1616
"repository": {
1717
"type": "git",

src/Index.hpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#ifndef NBS_INDEX_HPP
22
#define NBS_INDEX_HPP
33

4-
#include <filesystem>
54
#include <iostream>
65
#include <map>
6+
#include <sys/stat.h>
77
#include <vector>
88

99
#include "IndexItem.hpp"
1010
#include "TypeSubtype.hpp"
1111
#include "zstr/zstr.hpp"
1212

1313
namespace nbs {
14+
1415
class Index {
1516
public:
1617
/// Empty default constructor
@@ -24,17 +25,17 @@ namespace nbs {
2425
template <typename T>
2526
Index(const T& paths) {
2627
for (size_t i = 0; i < paths.size(); i++) {
27-
auto& nbsPath = paths[i];
28-
std::filesystem::path idxPath = nbsPath + ".idx";
28+
auto& nbsPath = paths[i];
29+
std::string idxPath = nbsPath + ".idx";
2930

3031
// Currently we only handle nbs files that have an index file
3132
// If there's no index file, throw
32-
if (!std::filesystem::exists(idxPath)) {
33+
if (!this->fileExists(idxPath)) {
3334
throw std::runtime_error("nbs index not found for file: " + nbsPath);
3435
}
3536

3637
// Load the index file
37-
zstr::ifstream input(idxPath.string());
38+
zstr::ifstream input(idxPath, zstr::ifstream::binary);
3839

3940
// Read the index items from the file
4041
while (input.good()) {
@@ -111,7 +112,9 @@ namespace nbs {
111112

112113
/// Get the first and last timestamps across all items in the index
113114
std::pair<uint64_t, uint64_t> getTimestampRange() {
114-
std::pair<uint64_t, uint64_t> range{std::numeric_limits<uint64_t>::max(), 0};
115+
// std::numeric_limits<uint64_t>::max is wrapped in () here to workaround an issue on Windows
116+
// See https://stackoverflow.com/a/27443191
117+
std::pair<uint64_t, uint64_t> range{(std::numeric_limits<uint64_t>::max)(), 0};
115118

116119
for (auto& mapEntry : this->typeMap) {
117120
const IndexItem& firstItem = mapEntry.second.first->item;
@@ -150,6 +153,13 @@ namespace nbs {
150153
/// Maps a (type, subtype) to the iterator over the index items for that type and subtype
151154
std::map<TypeSubtype, std::pair<std::vector<IndexItemFile>::iterator, std::vector<IndexItemFile>::iterator>>
152155
typeMap;
156+
157+
/// Check if a file exists at the given path
158+
bool fileExists(const std::string& path) {
159+
// Shamelessly stolen from: http://stackoverflow.com/a/12774387/1387006
160+
struct stat buffer {};
161+
return (stat(path.c_str(), &buffer) == 0);
162+
}
153163
};
154164

155165
} // namespace nbs

0 commit comments

Comments
 (0)