1 | [![npm version](https://badge.fury.io/js/metalsmith-changed.svg)](https://badge.fury.io/js/metalsmith-changed) [![Build Status](https://travis-ci.org/arve0/metalsmith-changed.svg?branch=master)](https://travis-ci.org/arve0/metalsmith-changed)
|
2 |
|
3 | # metalsmith-changed
|
4 | Only process files that have changed.
|
5 |
|
6 | metalsmith-changed will write a ctimes json-file to your `build`-folder in order to keep track of changed files.
|
7 |
|
8 | **Must** be used with `metalsmith.clean(false)`, `.clean(true)` (the default) disables metalsmith-changed and all files are passed on to the next plugin.
|
9 |
|
10 | metalsmith-changed can also be disabled with `force: true` and individual files can be ignored (always build) with `forcePattern`.
|
11 |
|
12 |
|
13 | ## example
|
14 | ```js
|
15 | var Metalsmith = require('metalsmith');
|
16 | var changed = require('metalsmith-changed');
|
17 |
|
18 | Metalsmith()
|
19 | .clean(false)
|
20 | .use(changed())
|
21 | ... // more plugins
|
22 | .build(function (err) {
|
23 | if (err) throw err;
|
24 | });
|
25 | ```
|
26 |
|
27 | Which is useful when watching files and livereloading:
|
28 | ```js
|
29 | const Metalsmith = require('metalsmith');
|
30 | const changed = require('metalsmith-changed');
|
31 | const livereload = require('metalsmith-livereload');
|
32 | const nodeStatic = require('node-static');
|
33 | const watch = require('glob-watcher');
|
34 | const open = require('open');
|
35 |
|
36 | const DIR = __dirname + '/test/fixtures/';
|
37 |
|
38 | /**
|
39 | * Build with metalsmith.
|
40 | */
|
41 | const build = (clean = false) => (done) => {
|
42 | console.log(`Building. clean: ${clean}.`);
|
43 | Metalsmith(DIR)
|
44 | .clean(clean)
|
45 | .use(changed())
|
46 | // .use(expensivePlugin()) // ie markdown -> html
|
47 | .use(livereload({ debug: true }))
|
48 | .build((err, files) => {
|
49 | let filenames = Object.keys(files).join(', ');
|
50 | console.log('Built: ' + filenames);
|
51 | done(err);
|
52 | });
|
53 | };
|
54 |
|
55 | /**
|
56 | * Serve files.
|
57 | */
|
58 | var serve = new nodeStatic.Server(DIR + 'build');
|
59 | require('http').createServer((req, res) => {
|
60 | req.addListener('end', () => serve.serve(req, res));
|
61 | req.resume();
|
62 | }).listen(8080);
|
63 |
|
64 | /**
|
65 | * Watch files.
|
66 | */
|
67 | watch(DIR + 'src/**/*', { ignoreInitial: false }, build(false));
|
68 | // watch(DIR + 'templates/**/*', build(true)); // force build of all files
|
69 |
|
70 | /**
|
71 | * Open browser.
|
72 | */
|
73 | open('http://localhost:8080');
|
74 | ```
|
75 |
|
76 | As ctimes are persisted to disk, this works nice with cli tools like [watch run](https://www.npmjs.com/package/watch-run) too.
|
77 |
|
78 | ## forcePattern
|
79 | If the option `forcePattern` is defined, files matching the pattern(s) will not
|
80 | be removed from building even if the file has not changed. `forcePattern` should
|
81 | be a string or an array of strings.
|
82 |
|
83 | [micromatch](https://github.com/jonschlinkert/micromatch) is used for
|
84 | matching the files.
|
85 |
|
86 | Example:
|
87 | ```js
|
88 | Metalsmith()
|
89 | .clean(false)
|
90 | .use(changed({
|
91 | forcePattern: [
|
92 | '**/index.md', // always build index files
|
93 | ... // more patterns
|
94 | ]
|
95 | }))
|
96 | ... // more plugins
|
97 | .build(function(err){
|
98 | if (err) throw err;
|
99 | });
|
100 | ```
|
101 |
|
102 |
|
103 | ## default options
|
104 | ```js
|
105 | changed({
|
106 | force: false, // build all files
|
107 | forcePattern: false, // always build files matching these patterns
|
108 | forceAllPattern: false, // force build of all files if files matching this pattern is changed
|
109 | ctimes: 'metalsmith-changed-ctimes.json' // where to store ctimes, relative to the build folder
|
110 | })
|
111 | ```
|
112 |
|
113 |
|
114 | ## metalsmith-changed-ctimes.json
|
115 | `metalsmith-changed-ctimes.json` is written to your `build` folder upon every build. `metalsmith-changed` takes ctimes from `files[n].stats.ctime`, so if a plugin creates files with `.stats.ctime`, `metalsmith-changed` can be used with it.
|
116 |
|
117 | Files without `stats.ctime` are always built.
|
118 |
|
119 |
|
120 | ## develop
|
121 | ```sh
|
122 | npm build # babel
|
123 | npm test
|
124 | DEBUG=metalsmith-changed npm test # test with debug output
|
125 | ```
|
126 |
|
127 | ## release
|
128 | ```sh
|
129 | npm version patch|minor|major
|
130 | npm publish
|
131 | ```
|