Skip to content

Commit

Permalink
Support Windows development (#195)
Browse files Browse the repository at this point in the history
* Port build and test scripts to Windows

* Port generate script to JS

* Use TypeScript for generate script

* Require glob after NPM install

* Alter generate to work before first install

* Remove unnecessary types

* Reinstall NPM packages using .nvmrc Node version
  • Loading branch information
tobyn authored and jonny-improbable committed Aug 16, 2019
1 parent c511ff3 commit 7a65c0d
Show file tree
Hide file tree
Showing 9 changed files with 1,985 additions and 112 deletions.
7 changes: 7 additions & 0 deletions bin/protoc-gen-ts.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\lib\index.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\lib\index.js" %*
)
101 changes: 101 additions & 0 deletions generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const { spawnSync } = require("child_process");

const { existsSync, mkdirSync } = require("fs");
const { resolve } = require("path");

const protocVersion = "3.5.1";

const examplesGeneratedPath = resolve(__dirname, "examples", "generated");

const binSuffix = process.platform === "win32" ? ".cmd" : "";
const nodeModulesBin = resolve(__dirname, "node_modules", ".bin");

const downloadPath = resolve(nodeModulesBin, "download") + binSuffix;

const protocRoot = resolve(__dirname, "protoc");
const protocPath = resolve(protocRoot, "bin", "protoc");

const protocPluginPath = resolve(__dirname, "bin", "protoc-gen-ts") + binSuffix;

const rimrafPath = resolve(nodeModulesBin, "rimraf") + binSuffix;

const supportedPlatforms = {
darwin: {
downloadSuffix: "osx-x86_64",
name: "Mac"
},
linux: {
downloadSuffix: "linux-x86_64",
name: "Linux"
},
win32: {
downloadSuffix: "win32",
name: "Windows"
}
};

const platform = supportedPlatforms[process.platform];
const platformName = platform ?
platform.name :
`UNKNOWN:${process.platform}`;
console.log("You appear to be running on", platformName);

requireBuild();

const glob = require("glob");

requireProtoc();

if (existsSync(examplesGeneratedPath)) {
run(rimrafPath, examplesGeneratedPath);
}

mkdirSync(examplesGeneratedPath);

run(protocPath,
`--proto_path=${__dirname}`,
`--plugin=protoc-gen-ts=${protocPluginPath}`,
`--js_out=import_style=commonjs,binary:${examplesGeneratedPath}`,
`--ts_out=service=true:${examplesGeneratedPath}`,
...glob.sync(resolve(__dirname, "proto", "**/*.proto"))
);

run(rimrafPath, protocRoot);

function requireBuild() {
console.log("Ensuring we have NPM packages installed...");
run("npm", "install");

console.log("Compiling ts-protoc-gen...");
run("npm", "run", "build");
}

function requireProtoc() {
if (existsSync(protocPath)) {
return;
}

if (!platform) {
throw new Error(
"Cannot download protoc. " +
platformName +
" is not currently supported by ts-protoc-gen"
);
}

console.log(`Downloading protoc v${protocVersion} for ${platform.name}`);
const protocUrl =
`https://github.com/google/protobuf/releases/download/v${protocVersion}/protoc-${protocVersion}-${platform.downloadSuffix}.zip`;

run(downloadPath,
"--extract",
"--out", protocRoot,
protocUrl);
}

function run(executablePath, ...args) {
const result = spawnSync(executablePath, args, { shell: true, stdio: "inherit" });
if (result.status !== 0) {
throw new Error(`Exited ${executablePath} with status ${result.status}`);
}
}
58 changes: 0 additions & 58 deletions generate.sh

This file was deleted.

Loading

0 comments on commit 7a65c0d

Please sign in to comment.