Skip to content

Commit

Permalink
Add date modified to file variable
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Aug 13, 2024
1 parent 9dcc8b2 commit 4e04c8d
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ The source files are processed by LiquidJS, so you can use [template outputs](ht
Custom variables are created with the [assign](ttps://liquidjs.com/tags/assign.html) tag.

Three special variables are available by default:
* `file` ([path information](https://nodejs.org/api/path.html#pathparsepath) about the target file)
* `file` ([path](https://nodejs.org/api/path.html#pathparsepath) and date information about the source file)
* `package` (values from your project's **package.json** file)
* `webRoot` (relative path to root folder: `.`, `..`, `../..`, `../../..`, etc.)

Expand All @@ -160,6 +160,8 @@ Example outputs and formatters:
| `{{file.base}}` | `sign-in.html` | Source filename with the file extension |
| `{{file.name}}` | `sign-in` | Source filename without the file extension |
| `{{file.ext}}` | `.html` | File extension of the source file |
| `{{file.modified}}` | `April 7, 2030` | Formatted date of when file was last modifiled |
| `{{file.date\|date:"%A"}}` | `Sunday` | Date object for when file was last modifiled |
| `<a href={{webRoot}}>Home</a>` | `<a href=../..>Home</a>` | Link is relative to the source folder |
| `{{"now"\|date:"%Y-%m-%d"}}` | `2024-01-21` | Build date timestamp |
| `{{myVariable\|upcase}}` | `DARK MODE` | Custom variable set with: `{% assign myVariable = 'dark mode' %}` |
Expand Down Expand Up @@ -216,7 +218,7 @@ See the **TypeScript Declarations** at the top of [replacer.ts](replacer.ts) for
- 🪺 [recursive-exec](https://github.com/center-key/recursive-exec):&nbsp; _Run a command on each file in a folder and its subfolders_
- 🔍 [replacer-util](https://github.com/center-key/replacer-util):&nbsp; _Find and replace strings or template outputs in text files_
- 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets):&nbsp; _Revision web asset filenames with cache busting content hash fingerprints_
- 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util):&nbsp; _Organize npm package.json scripts into named groups of easy to manage commands_
- 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util):&nbsp; _Organize npm package.json scripts into groups of easy to manage commands_
- 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator):&nbsp; _Check the markup validity of HTML files using the W3C validator_

Feel free to submit questions at:<br>
Expand Down
5 changes: 4 additions & 1 deletion replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ const replacer = {
const dir = slash(parsedPath.dir);
const filePath = dir + '/' + slash(parsedPath.base);
const folder = path.basename(dir);
return { ...parsedPath, dir: dir, folder: folder, path: filePath };
const date = fs.statSync(origin).mtime;
const dateFormat = { day: 'numeric', month: 'long', year: 'numeric' } as const;
const modified = date.toLocaleString([], dateFormat); //ex: "April 7, 2030"
return { ...parsedPath, dir: dir, folder: folder, path: filePath, date, modified };
};
const getWebRoot = (origin: string) => {
const depth = origin.substring(source.length).split('/').length - 2;
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/source/mock1.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<header>
<h1>🔍🔍🔍 {{package.name}} 🔍🔍🔍</h1>
<h2>{{package.description}}</h2>
<h3>Last Updated: {{file.modified}}</h3>
<h3>Weekday: {{file.date | date: "%A"}}</h3>
</header>
<main>
<p>{{slogan}}</p>
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/target/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion spec/fixtures/target/mock1.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<header>
<h1>🔍🔍🔍 replacer-util 🔍🔍🔍</h1>
<h2>Find and replace strings or template outputs in text files (CLI tool designed for use in npm package.json scripts)</h2>
<h3>Last Updated: August 12, 2024</h3>
<h3>Weekday: Monday</h3>
</header>
<main>
<p>I, for one, welcome our new A.I. module overlords.</p>
Expand All @@ -31,7 +33,7 @@ <h2>Find and replace strings or template outputs in text files (CLI tool designe
banner: '🔍🔍🔍 replacer-util v1.3.0 🔍🔍🔍',
description: 'Find and replace strings or template outputs in text files (CLI tool designed for use in npm package.json scripts)',
code: 'mock1',
file: '{"root":"","dir":"spec/fixtures/source","base":"mock1.html","ext":".html","name":"mock1","folder":"source","path":"spec/fixtures/source/mock1.html"}',
file: '{"root":"","dir":"spec/fixtures/source","base":"mock1.html","ext":".html","name":"mock1","folder":"source","path":"spec/fixtures/source/mock1.html","date":"2024-08-13T03:31:37.663Z","modified":"August 12, 2024"}',
year: '2024',
list1: 'A.I. module, A.I. module, A.I. module',
list2: 'A.I. module, iNsEcT, INSECT, A.I. module',
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/target/subfolder-a/mock2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const info2 = {
banner: '🔍🔍🔍 replacer-util v1.3.0 🔍🔍🔍',
description: 'Find and replace strings or template outputs in text files (CLI tool designed for use in npm package.json scripts)',
code: 'mock2',
file: '{"root":"","dir":"spec/fixtures/source/subfolder-a","base":"mock2.js","ext":".js","name":"mock2","folder":"subfolder-a","path":"spec/fixtures/source/subfolder-a/mock2.js"}',
file: '{"root":"","dir":"spec/fixtures/source/subfolder-a","base":"mock2.js","ext":".js","name":"mock2","folder":"subfolder-a","path":"spec/fixtures/source/subfolder-a/mock2.js","date":"2024-02-02T15:02:19.837Z","modified":"February 2, 2024"}',
year: '2024',
list1: 'A.I. module, A.I. module, A.I. module',
list2: 'A.I. module, iNsEcT, INSECT, A.I. module',
Expand Down
4 changes: 3 additions & 1 deletion spec/fixtures/target/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<header>
<h1>🔍🔍🔍 replacer-util 🔍🔍🔍</h1>
<h2>Find and replace strings or template outputs in text files (CLI tool designed for use in npm package.json scripts)</h2>
<h3>Last Updated: August 12, 2024</h3>
<h3>Weekday: Monday</h3>
</header>
<main>
<p>I, for one, welcome our new insect overlords.</p>
Expand All @@ -31,7 +33,7 @@ <h2>Find and replace strings or template outputs in text files (CLI tool designe
banner: '🔍🔍🔍 replacer-util v1.3.0 🔍🔍🔍',
description: 'Find and replace strings or template outputs in text files (CLI tool designed for use in npm package.json scripts)',
code: 'mock1',
file: '{"root":"","dir":"spec/fixtures/source","base":"mock1.html","ext":".html","name":"mock1","folder":"source","path":"spec/fixtures/source/mock1.html"}',
file: '{"root":"","dir":"spec/fixtures/source","base":"mock1.html","ext":".html","name":"mock1","folder":"source","path":"spec/fixtures/source/mock1.html","date":"2024-08-13T03:31:37.663Z","modified":"August 12, 2024"}',
year: '2024',
list1: 'insect, insect, insect',
list2: 'insect, iNsEcT, INSECT, insect',
Expand Down

0 comments on commit 4e04c8d

Please sign in to comment.