UNPKG

2.18 kBJavaScriptView Raw
1const { join } = require('path')
2const { readFile } = require('fs')
3const { isObject } = require('./lib/helpers')
4
5const outdatedPlugins = [
6 'markdown',
7 'in-place',
8 'layouts',
9 'collections',
10 'drafts',
11 'ignore',
12 'postcss',
13 'permalinks',
14 'default-values',
15 'metadata',
16 'excerpts',
17 'autotoc'
18]
19
20const renamedPlugins = {
21 ignore: 'remove',
22 autotoc: 'table-of-contents'
23}
24
25// only provided when running npm scripts
26// otherwise default to working directory
27const projectDir = process.env.INIT_CWD || process.cwd()
28
29// when reading package.json
30// always fail silently as to not interrupt the install DX
31readFile(join(projectDir, 'package.json'), 'utf-8', (err, data) => {
32 if (err) return
33 let parsed = false
34 try {
35 parsed = JSON.parse(data)
36 } catch (err) {
37 return
38 }
39 if (!parsed) return
40
41 const toCheck = []
42
43 if (isObject(parsed.devDependencies)) {
44 toCheck.push.apply(toCheck, Object.keys(parsed.devDependencies))
45 }
46 if (isObject(parsed.dependencies)) {
47 toCheck.push.apply(toCheck, Object.keys(parsed.dependencies))
48 }
49
50 const pluginMap = toCheck.reduce((all, fullname) => {
51 if (!fullname.startsWith('metalsmith-')) return all
52 const name = fullname.replace(/^metalsmith-/, '')
53 if (outdatedPlugins.includes(name)) {
54 const newName = renamedPlugins[name] ? renamedPlugins[name] : name
55 all[`metalsmith-${name}`] = `@metalsmith/${newName}`
56 }
57 return all
58 }, {})
59
60 if (Object.keys(pluginMap).length) {
61 process.stdout.write(
62 `\x1b[43m\x1b[30m WARN \x1b[0m\x1b[49m\x1b[33m Metalsmith has detected deprecated core plugins that have moved to the @metalsmith org on NPM:\n\n`
63 )
64 Object.entries(pluginMap).forEach(([oldName, newName]) => {
65 process.stdout.write(${oldName} -> ${newName}\n`)
66 })
67 process.stdout.write(
68 [
69 '\nRun the commands below to migrate:',
70 ` npm remove ${Object.keys(pluginMap).join(' ')}`,
71 ` npm i ${Object.values(pluginMap).join(' ')}\n`,
72 'For all available core plugins, see https://github.com/search?q=topic%3Ametalsmith-plugin+org%3Ametalsmith&type=Repositories',
73 '\x1b[0m'
74 ].join('\n')
75 )
76 }
77})