1 const {
2 posix: {join},
3 } = require('path')
4
5 // An object to generate redirects for a given path.
6 // The key is a glob path that is matched by minimatch,
7 // and the value is a function that is passed a parsed
8 // path with { section, page, release }.
9 //
10 // Return an absolute path to bypass the normal /cli/
11 // folder prefix to the redirect path. These will only
12 // be applied for the default release or if an object
13 // with `default: true` is also returned.
14 //
15 // The order of the keys is important because the result of
16 // one redirect can generate other redirects if they match
17 // one of the remaining globs. So keys should be ordered
18 // from the least specific -> most specific. Note that this
19 // order is reversed when actually parsing the object and
20 // running the code so it works properly.
21 //
22 // For example: path -> configuring-npm/package-lock-json
23 // This will first match: configuring-npm/*
24 // which will generate the redirects files/package-lock-json
25 // Later this will match: **/*-json*
26 // So both redirects from: configuring-npm/package-lock.json
27 // and files/package-lock.json will also be created.
28 //
29 // Note that all redirects are deduped later so it is safe
30 // if a path creates multiple idential redirects. Gatsby
31 // will also warn (but not create them) if multiple pages
32 // redirect to the same or already exsiting paths.
33
34 module.exports = {
35 index: () => ['.'],
36 '*/!(index)': ({section, page}) => [join(section, page), join('/', section, page)],
37 '*/index': ({section, page}) => [join(section), join('/', section), join(section, page), join('/', section, page)],
38
39 // any page with `-json` -> `.json`
40 '**/*-json*': ({section, page}) => [join(section, page.replace(/-json/g, '.json'))],
41
42 // commands
43 'commands/!(index)': ({page}) => [page],
44 'commands/index': () => ['/cli-documentation/cli'],
45 'commands/*': ({page}) => [join('cli-commands', page), join('/', 'cli-commands', page)],
46 'commands/npm-*': ({section, page}) => [join(section, page.replace(/^npm-/, ''))],
47
48 // configuring npm
49 'configuring-npm/*': ({page}) => [join('files', page), join('/', 'files', page)],
50 'configuring-npm/package-locks': ({section, page, release}) => [
51 // A special case for a path that was deleted in v7, but we still
52 // want the old v6 default url to resolve.
53 {path: join('/', section, page), default: release.id === 'v6'},
54 {path: join(section, page), default: release.id === 'v6'},
55 ],
56
57 // using npm
58 'using-npm/*': ({page}) => [join('misc', page), join('/', 'misc', page)],
59 'using-npm/removal': ({section}) => [join(section, 'removing-npm')],
60 'using-npm/scope': ({section}) => [join(section, 'npm-scope')],
61 'about-pgp-signatures-for-packages-in-the-public-registry': () => ['/about-registry-signatures'],
62 'verifying-the-pgp-signature-for-a-package-from-the-npm-public-registry': () => ['/verifying-registry-signatures'],
63 }