├── .npmignore ├── .gitignore ├── .editorconfig ├── .github └── workflows │ ├── ci.yml │ └── release.yml ├── package.json ├── LICENSE ├── README.md ├── create.js └── test └── test.js /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | .github 3 | sample 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | sample 3 | test/stubs* 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | insert_final_newline = true 7 | trim_trailing_whitespace = true 8 | charset = utf-8 9 | 10 | [*.yml] 11 | indent_style = space 12 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Node Unit Tests 2 | on: 3 | push: 4 | branches-ignore: 5 | - "gh-pages" 6 | jobs: 7 | build: 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | matrix: 11 | os: ["ubuntu-latest", "macos-latest", "windows-latest"] 12 | node: ["18", "20", "22", "24"] 13 | name: Node.js ${{ matrix.node }} on ${{ matrix.os }} 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: Setup node 17 | uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node }} 20 | # cache: npm 21 | - run: npm install 22 | - run: npm test 23 | env: 24 | YARN_GPG: no 25 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Publish Release to npm 2 | on: 3 | release: 4 | types: [published] 5 | permissions: read-all 6 | jobs: 7 | build: 8 | runs-on: ubuntu-latest 9 | environment: GitHub Publish 10 | permissions: 11 | contents: read 12 | id-token: write 13 | steps: 14 | - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # 4.1.7 15 | - uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # 4.0.3 16 | with: 17 | node-version: "22" 18 | registry-url: 'https://registry.npmjs.org' 19 | - run: npm install -g npm@latest 20 | - run: npm ci 21 | - run: npm test 22 | - if: ${{ github.event.release.tag_name != '' && env.NPM_PUBLISH_TAG != '' }} 23 | run: npm publish --provenance --access=public --tag=${{ env.NPM_PUBLISH_TAG }} 24 | env: 25 | NPM_PUBLISH_TAG: ${{ contains(github.event.release.tag_name, '-beta.') && 'beta' || 'latest' }} 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@11ty/create", 3 | "version": "1.0.9", 4 | "description": "A little command line utility to create files in a cross-platform way.", 5 | "main": "create.js", 6 | "type": "module", 7 | "engines": { 8 | "node": ">=18" 9 | }, 10 | "bin": { 11 | "@11ty/create": "create.js" 12 | }, 13 | "scripts": { 14 | "test": "node --test", 15 | "demo": "node . sample/index.md '# Heading'" 16 | }, 17 | "publishConfig": { 18 | "access": "public" 19 | }, 20 | "funding": { 21 | "type": "opencollective", 22 | "url": "https://opencollective.com/11ty" 23 | }, 24 | "author": "Zach Leatherman (https://zachleat.com/)", 25 | "repository": { 26 | "type": "git", 27 | "url": "git://github.com/11ty/create.git" 28 | }, 29 | "bugs": "https://github.com/11ty/create/issues", 30 | "homepage": "https://www.11ty.dev/", 31 | "license": "MIT", 32 | "dependencies": { 33 | "kleur": "^4.1.5" 34 | }, 35 | "devDependencies": { 36 | "rimraf": "^6.1.2" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Zach Leatherman @zachleat 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `@11ty/create` 2 | 3 | A tiny command line utility to create files in a cross-platform way. Requires Node 18 or newer. 4 | 5 | Some context about why this exists: [**The Smorgasbord of Windows Terminal… Windows**](https://www.zachleat.com/web/smorgasbord-windows-terminal/). 6 | 7 | --- 8 | 9 | In the quick start for Eleventy, there are different commands folks need to use based on their operating system and terminal application. 10 | 11 | ```sh 12 | # POSIX and PowerShell Core (Windows) 13 | echo '# Header' > index.md 14 | 15 | # Command Prompt (Windows cmd.exe, no quotes allowed) 16 | echo # Header > index.md 17 | 18 | # Windows PowerShell (`>` defaults to UTF16) 19 | echo '# Header' | out-file -encoding utf8 'index.md' 20 | ``` 21 | 22 | Now we can tell folks to use this everywhere and we’ll always have a UTF8 encoded file at the end: 23 | 24 | ```sh 25 | npx @11ty/create index.md '# Heading' 26 | npx @11ty/create nested/index.md '# Heading' 27 | ``` 28 | 29 | And in v1.0.6+ `stdin` is supported: 30 | ```sh 31 | echo '# Heading' | npx @11ty/create index.md 32 | ``` 33 | 34 | Installation happens on-the-fly via `npx` and we needn’t put this in a package.json. 35 | -------------------------------------------------------------------------------- /create.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | // I wish this didn’t have to exist https://fediverse.zachleat.com/@zachleat/112615813950390962 4 | 5 | import { parseArgs } from "node:util"; 6 | import path from "node:path"; 7 | import fs from "node:fs"; 8 | import chalk from "kleur"; 9 | import readline from "node:readline"; 10 | 11 | const SINGLE_QUOTE = "'"; 12 | const DOUBLE_QUOTE = '"'; 13 | 14 | async function getStdIn() { 15 | return new Promise((resolve, reject) => { 16 | let rl = readline.createInterface({ input: process.stdin }); 17 | let timer = setTimeout(() => { 18 | reject(); 19 | }, 100); 20 | rl.on("line", line => { 21 | resolve(line); 22 | clearTimeout(timer); 23 | }); 24 | rl.on("end", () => { 25 | reject(); 26 | }); 27 | }); 28 | } 29 | 30 | function stripQuotes(content) { 31 | let trimmed = content.trim(); 32 | if(trimmed.startsWith(SINGLE_QUOTE) && trimmed.endsWith(SINGLE_QUOTE) || trimmed.startsWith(DOUBLE_QUOTE) && trimmed.endsWith(DOUBLE_QUOTE)) { 33 | return trimmed.slice(1, -1); 34 | } 35 | return content; 36 | } 37 | 38 | function isExistingDir(filepath) { 39 | return fs.existsSync(filepath) && fs.statSync(filepath).isDirectory(); 40 | } 41 | 42 | function hasFilename(filepath) { 43 | if(filepath.endsWith(path.sep) || filepath.endsWith("/")) { 44 | return false; 45 | } 46 | 47 | let parsed = path.parse(filename); 48 | if(!parsed.base) { 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | let { positionals, values } = parseArgs({ 55 | allowPositionals: true, 56 | strict: false, 57 | options: { 58 | encoding: { 59 | type: "string", 60 | default: "utf8", 61 | }, 62 | quiet: { 63 | type: "boolean", 64 | default: false, 65 | }, 66 | }, 67 | }); 68 | 69 | // TODO check not a parent directory 70 | 71 | let { quiet, encoding } = values; 72 | let [ filename, content ] = positionals; 73 | let src; 74 | 75 | if(!content) { 76 | try { 77 | content = await getStdIn(); 78 | src = "stdin"; 79 | } catch(e) { 80 | // do nothing 81 | } 82 | 83 | // for Windows cmd.exe 84 | content = stripQuotes(content); 85 | } 86 | 87 | // Input checking 88 | if(!filename || !content || !hasFilename(filename) || isExistingDir(filename)) { 89 | console.error("Incorrect usage, expected one of:"); 90 | console.error(" npx @11ty/create file_path 'File Content'"); 91 | console.error(" echo 'File Content' | npx @11ty/create file_path"); 92 | process.exit(1); 93 | } 94 | 95 | // Create parent dirs 96 | let dirs = filename.split(path.sep); 97 | 98 | // On Windows, work with forward and back slashes 99 | if(dirs.length <= 1 && path.sep !== "/" && filename.includes("/")) { 100 | dirs = filename.split("/"); 101 | } 102 | 103 | if(dirs.length > 1 ) { 104 | dirs.pop(); 105 | 106 | fs.mkdirSync(dirs.join(path.sep), { 107 | recursive: true 108 | }); 109 | } 110 | 111 | // Write file 112 | fs.writeFileSync(filename, content, { encoding }); 113 | 114 | // Output 115 | if(!quiet) { 116 | console.log( `${chalk.gray('[11ty/create]')} Writing ${filename} ${chalk.gray(`(${(content.length/1000).toFixed(3)}kb)${src ? ` (${src})` : ""}`)}` ); 117 | } 118 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import test from 'node:test'; 2 | import assert from "node:assert/strict"; 3 | import fs from "node:fs"; 4 | import { spawn } from "node:child_process"; 5 | import { rimrafSync } from "rimraf"; 6 | 7 | const SAMPLE_CONTENT = '# Test content'; 8 | 9 | function waitForStreamClose(stream) { 10 | return new Promise(resolve => { 11 | stream.on("close", () => { 12 | resolve(); 13 | }); 14 | }); 15 | } 16 | 17 | test('Baseline double quotes', async (t) => { 18 | t.after(() => { 19 | rimrafSync("./test/stubs-dbl/"); 20 | }); 21 | 22 | let path = 'test/stubs-dbl/index.md'; 23 | let create = spawn("node", ['.', path, SAMPLE_CONTENT]); 24 | await waitForStreamClose(create); 25 | 26 | assert.ok(fs.existsSync(path)); 27 | assert.deepEqual(fs.readFileSync(path, "utf8"), SAMPLE_CONTENT); 28 | }); 29 | 30 | test('Baseline single quotes', async (t) => { 31 | t.after(() => { 32 | rimrafSync("./test/stubs-sgl/"); 33 | }); 34 | 35 | let path = 'test/stubs-sgl/index.md'; 36 | let create = spawn("node", ['.', path, SAMPLE_CONTENT]); 37 | 38 | await waitForStreamClose(create); 39 | 40 | assert.ok(fs.existsSync(path)); 41 | assert.deepEqual(fs.readFileSync(path, "utf8"), SAMPLE_CONTENT); 42 | }); 43 | 44 | test('Missing content', async (t) => { 45 | t.after(() => { 46 | rimrafSync("./test/stubs-err-c/"); 47 | }); 48 | 49 | let path = 'test/stubs-err-c/index.md'; 50 | let create = spawn("node", ['.', path]); 51 | await waitForStreamClose(create); 52 | 53 | assert.ok(!fs.existsSync(path)); 54 | }); 55 | 56 | test('Missing filename', async (t) => { 57 | t.after(() => { 58 | rimrafSync("./test/stubs-err-f/"); 59 | }); 60 | 61 | let path = 'test/stubs-err-f/'; 62 | let create = spawn("node", ['.', path]); 63 | await waitForStreamClose(create); 64 | 65 | assert.ok(!fs.existsSync(path)); 66 | }); 67 | 68 | test('Point to a directory', async (t) => { 69 | t.after(() => { 70 | rimrafSync("./test/stubs-err-d/"); 71 | }); 72 | 73 | let path = 'test/stubs-err-d/'; 74 | let create = spawn("node", ['.', path, SAMPLE_CONTENT]); 75 | await waitForStreamClose(create); 76 | 77 | assert.ok(!fs.existsSync(path)); 78 | }); 79 | 80 | test('Nested create', async (t) => { 81 | t.after(() => { 82 | rimrafSync("./test/stubs-nested/"); 83 | }); 84 | 85 | let path = 'test/stubs-nested/nested/index.md'; 86 | let create = spawn("node", ['.', path, SAMPLE_CONTENT]); 87 | await waitForStreamClose(create); 88 | 89 | assert.ok(fs.existsSync(path)); 90 | assert.deepEqual(fs.readFileSync(path, "utf8"), SAMPLE_CONTENT); 91 | }); 92 | 93 | test('Works with stdin', async (t) => { 94 | t.after(() => { 95 | rimrafSync("./test/stubs-stdin/"); 96 | }); 97 | 98 | let echo = spawn('echo', [SAMPLE_CONTENT]); 99 | let path = 'test/stubs-stdin/index.md'; 100 | let create = spawn("node", ['.', path]); 101 | 102 | echo.stdout.pipe(create.stdin); 103 | 104 | await waitForStreamClose(create); 105 | 106 | assert.deepEqual(fs.readFileSync(path, "utf8"), SAMPLE_CONTENT); 107 | }); 108 | 109 | test('Remove single quotes on stdin', async (t) => { 110 | t.after(() => { 111 | rimrafSync("./test/stubs-stdin-single-quote/"); 112 | }); 113 | 114 | let echo = spawn('echo', [`'${SAMPLE_CONTENT}'`]); 115 | let path = 'test/stubs-stdin-single-quote/index.md'; 116 | let create = spawn("node", ['.', path]); 117 | 118 | echo.stdout.pipe(create.stdin); 119 | 120 | await waitForStreamClose(create); 121 | 122 | assert.deepEqual(fs.readFileSync(path, "utf8"), SAMPLE_CONTENT); 123 | }); 124 | 125 | test('Remove double quotes on stdin', async (t) => { 126 | t.after(() => { 127 | rimrafSync("./test/stubs-stdin-double-quote/"); 128 | }); 129 | 130 | let echo = spawn('echo', [`"${SAMPLE_CONTENT}"`]); 131 | let path = 'test/stubs-stdin-double-quote/index.md'; 132 | let create = spawn("node", ['.', path]); 133 | 134 | echo.stdout.pipe(create.stdin); 135 | 136 | await waitForStreamClose(create); 137 | 138 | assert.deepEqual(fs.readFileSync(path, "utf8"), SAMPLE_CONTENT); 139 | }); 140 | 141 | test('Remove single quotes (whitespace) on stdin', async (t) => { 142 | t.after(() => { 143 | rimrafSync("./test/stubs-stdin-single-quote/"); 144 | }); 145 | 146 | let echo = spawn('echo', [` '${SAMPLE_CONTENT}' `]); 147 | let path = 'test/stubs-stdin-single-quote/index.md'; 148 | let create = spawn("node", ['.', path]); 149 | 150 | echo.stdout.pipe(create.stdin); 151 | 152 | await waitForStreamClose(create); 153 | 154 | assert.deepEqual(fs.readFileSync(path, "utf8"), SAMPLE_CONTENT); 155 | }); 156 | 157 | test('Remove double quotes (whitespace) on stdin', async (t) => { 158 | t.after(() => { 159 | rimrafSync("./test/stubs-stdin-double-quote/"); 160 | }); 161 | 162 | let echo = spawn('echo', [` "${SAMPLE_CONTENT}" `]); 163 | let path = 'test/stubs-stdin-double-quote/index.md'; 164 | let create = spawn("node", ['.', path]); 165 | 166 | echo.stdout.pipe(create.stdin); 167 | 168 | await waitForStreamClose(create); 169 | 170 | assert.deepEqual(fs.readFileSync(path, "utf8"), SAMPLE_CONTENT); 171 | }); 172 | --------------------------------------------------------------------------------