{"id":15368254,"url":"https://github.com/keithamus/hashmark","last_synced_at":"2025-04-06T00:10:09.125Z","repository":{"id":22619717,"uuid":"25962131","full_name":"keithamus/hashmark","owner":"keithamus","description":"Take contents of a file (or stdin), and output as new file with a hash in the name","archived":false,"fork":false,"pushed_at":"2020-05-24T20:05:10.000Z","size":125,"stargazers_count":195,"open_issues_count":8,"forks_count":20,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-29T22:10:10.203Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/keithamus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-30T08:48:15.000Z","updated_at":"2024-05-21T17:36:03.000Z","dependencies_parsed_at":"2022-08-21T08:30:51.799Z","dependency_job_id":null,"html_url":"https://github.com/keithamus/hashmark","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keithamus%2Fhashmark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keithamus%2Fhashmark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keithamus%2Fhashmark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keithamus%2Fhashmark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/keithamus","download_url":"https://codeload.github.com/keithamus/hashmark/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247415972,"owners_count":20935387,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T13:28:50.994Z","updated_at":"2025-04-06T00:10:09.092Z","avatar_url":"https://github.com/keithamus.png","language":"JavaScript","funding_links":[],"categories":["Other Utilities"],"sub_categories":["Utility Packs"],"readme":"# HashMark\n\n[![Build Status](https://travis-ci.org/keithamus/hashmark.svg)](https://travis-ci.org/keithamus/hashmark)\n\n\u003ca target='_blank' rel='nofollow' href='https://app.codesponsor.io/link/ygkcNhfZ9nTDeVM6P8LSGn1C/keithamus/hashmark'\u003e  \u003cimg alt='Sponsor' width='888' height='68' src='https://app.codesponsor.io/embed/ygkcNhfZ9nTDeVM6P8LSGn1C/keithamus/hashmark.svg' /\u003e\u003c/a\u003e\n\nHashMark is a small utility which takes a file (or sdtin) as input, and writes\nthe contents of the input to a file named with a hash digest of the file. This\nis useful for cache busting sticky caches - files can have far future expires\nheaders and when the code changes, a new filename is created.\n\n## Examples:\n\n### Shell\n\n```bash\ncat file.js | ./bin/hashmark 'file.{hash}.js' # Writes to test.3eae1599bb7f187b86d6427942d172ba8dd7ee5962aab03e0839ad9d59c37eb0.js\n\u003e Computed hash: 3eae1599bb7f187b86d6427942d172ba8dd7ee5962aab03e0839ad9d59c37eb0\n\u003e\ncat file.js | ./bin/hashmark -l 8 'file.{hash}.js' # Writes to test.3eae1599.js\n\u003e Computed hash: 3eae1599\n\u003e\ncat file.js | ./bin/hashmark -l 4 -d md5 'dist/{hash}.js' # Writes to dist/cbd8.js\n\u003e Computed hash: cbd8\n\u003e\n```\n\nIt is useful to use globs — meaning you can read in many files and it will output\nhashed versions of each:\n\n```bash\n./bin/hashmark path/to/*.js 'dist/{name}.{hash}.js'\n./bin/hashmark path/to/{filea,fileb,filec}.js 'dist/{name}.{hash}.js'\n./bin/hashmark **/*.js 'dist/{dir}/{name}.{hash}.js'\n./bin/hashmark **/*.{js,css} 'dist/{dir}/{name}.{hash}{ext}'\n./bin/hashmark **/*.{js,css} 'dist/{dir}/{hash}-{base}'\n```\n\nThe above even works in shells that do not support globs (such as cmd.exe on\nWindows), because `hashmark` has support for expanding globs itself. Note that\nif your shell supports the `*` glob but not the `**` glob (such as bash before\nversion 4), the shell will interpret `**` as two `*` in a row, which means that\nhashmark never receives the `**` and therefore cannot expand it. In that case\nyou need to quote your argument. Therefore, if you want to write cross-platform\nscripts it is best to always quote the args:\n\nAvailable pattern keys includes all the keys returned by [`path.parse(filename)`](https://nodejs.org/api/path.html#path_path_parse_pathstring) and `hash` (ie: `hash`, `root`, `dir`, `name`, `base`, `ext`).\n\n```bash\n./bin/hashmark 'dir/**/*.{js,css}' 'test/*.js' 'dist/{dir}/{name}.{hash}{ext}'\n```\n\nThe `hashmark` command will output some JSON to stdout with a map of filenames\nand their new hashes, meaning you can pipe the hash to other programs. To make\n`hashmark` completely silent - simply pass the `--silent` or `-s` flag.\n\n```bash\n./bin/hashmark -l 4 file.js 'dist/{hash}.js' --silent\n```\n\nYou can also output the JSON map to a file, by passing the `--asset-map` or `-m`\nflag. It will still be logged to stdout unless you pass `--silent`\n\n```bash\n./bin/hashmark -l 4 file.js 'dist/{hash}.js' --asset-map assets.json\n```\n\nYou can specify from which directory to work from with `--cwd` or `-c`. _Note:_ `asset-map` will be relative to this directory.\n\n```bash\nmkdir dist/subdir\necho 'abracadabra' \u003e dist/subdir/file.js\n./bin/hashmark --cwd dist -d md5 -l 8 '**/*.js' '{dir}/{name}-{hash}{ext}'\n\u003e {\"subdir/file.js\":\"subdir/file-97640ef5.js\"}\n```\n\n### Integrations\n\n**[replaceinfiles](https://github.com/songkick/replaceinfiles)**\n\nNow that your assets have been renamed, you might want to update references to them in some other files _(ex:background image reference in your css files)_. That's exactly what `replaceinfiles` is made for. Just pipe it to `hashmark`. _It just work™._ [Full example](https://github.com/songkick/replaceinfiles/tree/master/examples/hashmark).\n\n### Programmatically\n\nThe hashmark function can be used programmatically. You can pass it a String,\nBuffer or Stream as the first argument, an options object as the second\nargument, and a callback as the third.\n\nThe callback receives an error as the first argument (or null) and an object\nwhich maps each given file to the newly hashed file name.\n\n```js\nvar hashmark = require('hashmark');\nvar file = fs.createReadStream('file.js');\n\nhashmark(file, { length: 8, digest: 'md5', 'pattern': '{hash}'}, function (err, map) {\n    console.log(map);\n});\n```\n\nThe function also returns an event emitter which emits `error`, `file` and `end`\nevents. File events get fired when an individual file has been hashed, and the\n`end` event is fired when all have been done. `file` is given two arguments -\nthe files old name, and the new calculated filename (given the template string),\nand the `end` event is given an object mapping of all files.\n\n```js\nvar hashmark = require('hashmark');\nvar file = fs.createReadStream('file.js');\n\nhashmark(file, { length: 8, digest: 'md5', pattern: 'hash'})\n    .on('file', function (oldFileName, newFileName) {\n        console.log('File hashed!', oldFileName, newFileName);\n    })\n    .on('end', function (jsonMap) {\n        console.log('~FIN');\n    })\n```\n\nFiles can be a single Stream, or filename String, or an Array of Streams and/or\nfilename Strings.\n\n```js\nvar hashmark = require('hashmark');\nvar file = fs.createReadStream('file.js');\n\nhashmark([file, 'file2.js'], { length: 8, digest: 'md5', file: 'file.#.js'}, function (err, hash) {\n    console.log('File written to file.' + hash + '.js');\n    console.log(hash);\n});\n```\n\n## Contributing\n\nThis is such a small utility - there's very little point in contributing. If it\ndoesn't do something that you really think it should, feel free to raise an\nissue - just be aware I might say no. If you can make it faster/better/stronger\nwithout changing the API/functionality then send a PR!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeithamus%2Fhashmark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeithamus%2Fhashmark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeithamus%2Fhashmark/lists"}