Skip to content

Commit fec2da6

Browse files
authored
make npx work (#2)
1 parent fd93f81 commit fec2da6

File tree

9 files changed

+306
-9
lines changed

9 files changed

+306
-9
lines changed

LICENSE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright 2024 FlexStack Contributors. All rights reserved.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8+

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Autogenerate a Dockerfile
2+
3+
FlexStack's `new-dockerfile` CLI tool and Go package automatically generates a configurable Dockerfile
4+
based on your project source code. It supports a wide range of languages and frameworks, including Next.js,
5+
Node.js, Python, Ruby, Java/Spring Boot, Go, Elixir/Phoenix, and more.
6+
7+
For detailed documentation, visit the [FlexStack Documentation](https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile) page.
8+
9+
## Installation
10+
11+
### cURL
12+
13+
```sh
14+
curl -sSL https://flexstack.com/install/new-dockerfile | sh
15+
```
16+
17+
### Go Binary
18+
19+
```sh
20+
go install github.com/flexstack/new-dockerfile@latest
21+
```
22+
23+
### Go Package
24+
25+
```sh
26+
go get github.com/flexstack/new-dockerfile
27+
```
28+
29+
## Supported Platforms for CLI
30+
31+
- **macOS** (arm64, x86_64)
32+
- **Linux** (arm64, x86_64)
33+
- **Windows** (x86_64, i386)
34+
35+
## CLI Usage
36+
37+
```sh
38+
new-dockerfile [options]
39+
```
40+
41+
## CLI Options
42+
43+
- `--path` - Path to the project source code (default: `.`)
44+
- `--write` - Write the generated Dockerfile to the project at the specified path (default: `false`)
45+
- `--runtime` - Force a specific runtime, e.g. `node` (default: `auto`)
46+
- `--quiet` - Disable all logging except for errors (default: `false`)
47+
- `--help` - Show help

cmd/new-dockerfile/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func main() {
1818
var path string
1919
flag.StringVar(&path, "path", ".", "Path to the project directory")
2020
var noColor bool
21-
flag.BoolVar(&noColor, "no-color", false, "Disable colorized output")
21+
flag.BoolVar(&noColor, "no-color", os.Getenv("NO_COLOR") == "true" || os.Getenv("ANSI_COLORS_DISABLED") == "true", "Disable colorized output")
2222
var runtimeArg string
2323
flag.StringVar(&runtimeArg, "runtime", "", "Force a specific runtime")
2424
var quiet bool

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/flexstack/new-dockerfile
22

3-
go 1.22.3
3+
go 1.21
4+
5+
toolchain go1.22.3
46

57
require (
68
github.com/lmittmann/tint v1.0.4

node/LICENSE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright 2024 FlexStack Contributors. All rights reserved.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8+

node/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1-
# Autogenerate a Dockerfile from your project source code
1+
# Autogenerate a Dockerfile
2+
3+
FlexStack's `new-dockerfile` CLI tool automatically generates a configurable Dockerfile based on your project source code.
4+
It supports a wide range of languages and frameworks, including Next.js, Node.js, Python, Ruby, Java/Spring Boot, Go,
5+
Elixir/Phoenix, and more.
6+
7+
For detailed documentation, visit the [FlexStack Documentation](https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile) page.
8+
9+
## Supported Platforms
10+
11+
- **macOS** (arm64, x86_64)
12+
- **Linux** (arm64, x86_64)
13+
- **Windows** (x86_64, i386)
14+
15+
## Usage
16+
17+
```sh
18+
npx new-dockerfile [options]
19+
```
20+
21+
## Options
22+
23+
- `--path` - Path to the project source code (default: `.`)
24+
- `--write` - Write the generated Dockerfile to the project at the specified path (default: `false`)
25+
- `--runtime` - Force a specific runtime, e.g. `node` (default: `auto`)
26+
- `--quiet` - Disable all logging except for errors (default: `false`)
27+
- `--help` - Show help

node/bin/new-dockerfile

3.5 MB
Binary file not shown.

node/install.js

Lines changed: 208 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,208 @@
1-
// TODO:
2-
// 1. Determine the platform and architecture of the system
3-
// 2. Determine the version of the npm package
4-
// 3. Download the Golang binary for the platform and architecture from GitHub releases
5-
console.log("Coming soon...");
1+
// Credits:
2+
// - Bun.js (https://github.com/oven-sh/bun/blob/main/packages/bun-release/src)
3+
const child_process = require("child_process");
4+
const { unzipSync } = require("zlib");
5+
const packageJson = require("./package.json");
6+
const fs = require("fs");
7+
8+
function getPlatform() {}
9+
10+
async function downloadCli(version, platform) {
11+
const ext = platform.os === "win32" ? ".zip" : ".tar.gz";
12+
const response = await fetch(
13+
`https://github.com/flexstack/new-dockerfile/releases/download/v${version}/${platform.bin}${ext}`
14+
);
15+
const tgz = await response.arrayBuffer();
16+
let buffer;
17+
18+
try {
19+
buffer = unzipSync(tgz);
20+
} catch (cause) {
21+
throw new Error("Invalid gzip data", { cause });
22+
}
23+
24+
function str(i, n) {
25+
return String.fromCharCode(...buffer.subarray(i, i + n)).replace(
26+
/\0.*$/,
27+
""
28+
);
29+
}
30+
let offset = 0;
31+
const dst = platform.exe;
32+
while (offset < buffer.length) {
33+
const name = str(offset, 100).replace("package/", "");
34+
const size = parseInt(str(offset + 124, 12), 8);
35+
offset += 512;
36+
if (!isNaN(size)) {
37+
write(dst, buffer.subarray(offset, offset + size));
38+
if (name === platform.exe) {
39+
try {
40+
fs.chmodSync(dst, 0o755);
41+
} catch (error) {}
42+
}
43+
offset += (size + 511) & ~511;
44+
}
45+
}
46+
}
47+
48+
const fetch = "fetch" in globalThis ? webFetch : nodeFetch;
49+
50+
async function webFetch(url, options) {
51+
const response = await globalThis.fetch(url, options);
52+
if (options?.assert !== false && !isOk(response.status)) {
53+
try {
54+
await response.text();
55+
} catch {}
56+
throw new Error(`${response.status}: ${url}`);
57+
}
58+
return response;
59+
}
60+
61+
async function nodeFetch(url, options) {
62+
const { get } = await import("node:http");
63+
return new Promise((resolve, reject) => {
64+
get(url, (response) => {
65+
const status = response.statusCode ?? 501;
66+
if (response.headers.location && isRedirect(status)) {
67+
return nodeFetch(url).then(resolve, reject);
68+
}
69+
if (options?.assert !== false && !isOk(status)) {
70+
return reject(new Error(`${status}: ${url}`));
71+
}
72+
const body = [];
73+
response.on("data", (chunk) => {
74+
body.push(chunk);
75+
});
76+
response.on("end", () => {
77+
resolve({
78+
ok: isOk(status),
79+
status,
80+
async arrayBuffer() {
81+
return Buffer.concat(body).buffer;
82+
},
83+
async text() {
84+
return Buffer.concat(body).toString("utf-8");
85+
},
86+
async json() {
87+
const text = Buffer.concat(body).toString("utf-8");
88+
return JSON.parse(text);
89+
},
90+
});
91+
});
92+
}).on("error", reject);
93+
});
94+
}
95+
96+
function isOk(status) {
97+
return status >= 200 && status <= 204;
98+
}
99+
100+
function isRedirect(status) {
101+
switch (status) {
102+
case 301: // Moved Permanently
103+
case 308: // Permanent Redirect
104+
case 302: // Found
105+
case 307: // Temporary Redirect
106+
case 303: // See Other
107+
return true;
108+
}
109+
return false;
110+
}
111+
112+
const os = process.platform;
113+
114+
const arch =
115+
os === "darwin" && process.arch === "x64" && isRosetta2()
116+
? "arm64"
117+
: process.arch;
118+
119+
const platforms = [
120+
{
121+
os: "darwin",
122+
arch: "x64",
123+
bin: "new-dockerfile-darwin-x86_64",
124+
exe: "bin/new-dockerfile",
125+
},
126+
{
127+
os: "darwin",
128+
arch: "arm64",
129+
bin: "new-dockerfile-darwin-arm64",
130+
exe: "bin/new-dockerfile",
131+
},
132+
{
133+
os: "linux",
134+
arch: "x64",
135+
bin: "new-dockerfile-linux-x86_64",
136+
exe: "bin/new-dockerfile",
137+
},
138+
{
139+
os: "linux",
140+
arch: "arm64",
141+
bin: "new-dockerfile-linux-arm64",
142+
exe: "bin/new-dockerfile",
143+
},
144+
{
145+
os: "win32",
146+
arch: "x64",
147+
bin: "new-dockerfile-windows-x86_64",
148+
exe: "bin/new-dockerfile.exe",
149+
},
150+
{
151+
os: "win32",
152+
arch: "arm64",
153+
bin: "new-dockerfile-windows-arm64",
154+
exe: "bin/new-dockerfile.exe",
155+
},
156+
];
157+
158+
const supportedPlatforms = platforms.filter(
159+
(platform) => platform.os === os && platform.arch === arch
160+
);
161+
162+
function isRosetta2() {
163+
try {
164+
const { exitCode, stdout } = spawn("sysctl", [
165+
"-n",
166+
"sysctl.proc_translated",
167+
]);
168+
return exitCode === 0 && stdout.includes("1");
169+
} catch (error) {
170+
return false;
171+
}
172+
}
173+
174+
function spawn(cmd, args, options = {}) {
175+
const { status, stdout, stderr } = child_process.spawnSync(cmd, args, {
176+
stdio: "pipe",
177+
encoding: "utf-8",
178+
...options,
179+
});
180+
return {
181+
exitCode: status ?? 1,
182+
stdout,
183+
stderr,
184+
};
185+
}
186+
187+
function write(dst, content) {
188+
try {
189+
fs.writeFileSync(dst, content);
190+
return;
191+
} catch (error) {
192+
// If there is an error, ensure the parent directory
193+
// exists and try again.
194+
try {
195+
fs.mkdirSync(path.dirname(dst), { recursive: true });
196+
} catch (error) {
197+
// The directory could have been created already.
198+
}
199+
fs.writeFileSync(dst, content);
200+
}
201+
}
202+
203+
if (supportedPlatforms.length === 0) {
204+
throw new Error("Unsupported platform: " + os + " " + arch);
205+
}
206+
207+
// Read version from package.json
208+
downloadCli(packageJson.config.bin_version, supportedPlatforms[0]);

node/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "new-dockerfile",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
4+
"config": {
5+
"bin_version": "0.1.0"
6+
},
47
"description": "Autogenerate Dockerfiles from your project source code",
58
"main": "index.js",
69
"bin": {

0 commit comments

Comments
 (0)