Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

remove-unused-assets.js 2.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const findUnusedAssets = require('./helpers/find-unused-assets')
  5. // [start-readme]
  6. //
  7. // Run this script to remove reusables and image files that exist in the repo but
  8. // are not used in content files. It also displays a list of unused variables. Set
  9. // the `--dry-run` to flag to print results without deleting any files. For images
  10. // you don't want to delete, add them to `ignoreList` in `lib/find-unused-assets.js`
  11. //
  12. // [end-readme]
  13. const dryRun = process.argv.slice(2).includes('--dry-run')
  14. main()
  15. async function main () {
  16. if (dryRun) {
  17. console.log('This is a dry run! The script will report unused files without deleting anything.')
  18. }
  19. removeUnusedReusables(await findUnusedAssets('reusables'))
  20. removeUnusedImages(await findUnusedAssets('images'))
  21. printUnusedVariables(await findUnusedAssets('variables'))
  22. }
  23. function removeUnusedReusables (reusables) {
  24. logMessage(reusables, 'reusable')
  25. reusables.forEach(reusable => {
  26. const reusablePath = path.join(__dirname, '..', reusable
  27. .replace('site', '')
  28. .replace(/\./g, '/')
  29. .replace(/$/, '.md'))
  30. dryRun
  31. ? console.log(reusable)
  32. : fs.unlinkSync(reusablePath)
  33. })
  34. }
  35. function removeUnusedImages (images) {
  36. logMessage(images, 'image')
  37. images.forEach(image => {
  38. const imagePath = path.join(__dirname, '..', image)
  39. dryRun
  40. ? console.log(image)
  41. : fs.unlinkSync(imagePath)
  42. })
  43. }
  44. // multiple variables are embedded in within the same YML file
  45. // so we can't just delete the files, and we can't parse/modify
  46. // them either because js-yaml does not preserve whitespace :[
  47. function printUnusedVariables (variables) {
  48. logMessage(variables, 'variable')
  49. variables.forEach(variable => {
  50. const variableKey = variable.split('.').pop()
  51. const variablePath = path.join(process.cwd(), variable
  52. .replace('site', '')
  53. .replace(`.${variableKey}`, '')
  54. .replace(/\./g, '/')
  55. .replace(/$/, '.yml'))
  56. dryRun
  57. ? console.log(variable)
  58. : console.log(`* found but did not delete '${variableKey}' in ${variablePath.replace(process.cwd(), '')}`)
  59. })
  60. if (!dryRun) console.log('\nYou will need to manually delete any variables you want to remove.')
  61. }
  62. function logMessage (list, type) {
  63. let action
  64. if (dryRun) {
  65. action = '\n**Found'
  66. } else {
  67. action = type === 'variable'
  68. ? ':eyes: **Found'
  69. : ':scissors: **Removed'
  70. }
  71. console.log(`${action} ${list.length} unused ${type} ${list.length === 1 ? 'file' : 'files'}**\n`)
  72. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...