Skip to content

Commit

Permalink
generate-rst script now will update civic-docs aliases rst if found
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcmichael committed Aug 22, 2023
1 parent a8c7474 commit b7447bc
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 19 deletions.
4 changes: 2 additions & 2 deletions client/scripts/generate-icon-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const tree = directoryTree(iconsDirectory, { extensions: /\.svg$/ })

if (!tree) {
console.error(
`Error: The directory ${iconsDirectory} does not exist or could not be accessed.`
`Error: The icon assets folder ${iconsDirectory} does not exist or could not be accessed.`
)
process.exit(1)
}
Expand All @@ -61,4 +61,4 @@ const outputPath = path.join(
// Write the JSON data to the file
fs.writeFileSync(outputPath, JSON.stringify(output, null, 2))

console.log(`Data written to ${outputPath}`)
console.log(`Icon data written to ${outputPath}`)
128 changes: 113 additions & 15 deletions client/scripts/generate-icon-rst.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
#!/usr/bin/env node
const directoryTree = require('directory-tree')
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const mustache = require('mustache')

// Load the generated icon data JSON
const jsonDataPath = path.join(
const iconsDirectory = path.join(__dirname, '..', 'src', 'assets', 'icons')
const validSubdirectories = ['attribute', 'outline', 'twotone', 'fullcolor']

function removeRedundantName(type, filename) {
const pattern =
type === 'attribute' ? /-outline$/ : new RegExp(`-${type}$`, 'i')
return filename.replace(pattern, '')
}

function generateIconObject(tree) {
if (!tree || !tree.children) return []

return tree.children
.filter((child) => validSubdirectories.includes(child.name))
.flatMap((subdir) => {
return subdir.children.map((file) => {
const cleanName = removeRedundantName(
subdir.name,
path.basename(file.name, '.svg')
)
return {
type: subdir.name,
filepath: `${subdir.name}/${file.name}`,
name: cleanName,
alias: `${subdir.name}-${cleanName}`,
}
})
})
}

const tree = directoryTree(iconsDirectory, { extensions: /\.svg$/ })

if (!tree) {
console.error(
`Error: The directory ${iconsDirectory} does not exist or could not be accessed.`
)
process.exit(1)
}

const icons = generateIconObject(tree)
const output = {
icons: icons,
}

const outputPath = path.join(
__dirname,
'..',
'src',
'app',
'generated',
'civic.icons.data.json'
)
const iconData = JSON.parse(fs.readFileSync(jsonDataPath, 'utf-8'))
fs.writeFileSync(outputPath, JSON.stringify(output, null, 2))
console.log(`Data written to ${outputPath}`)

// New part for checking civic-docs repository and generating RST

const docsRepoPath = path.join(__dirname, '..', '..', '..', 'civic-docs')
const targetDirectory = path.join(docsRepoPath, 'docs', 'generated')
const targetFile = path.join(targetDirectory, 'civic-docs.aliases.rst')

// Define the mustache template
const template = `
Expand All @@ -32,17 +84,63 @@ const renderedRst =
'Produced by `generate-icon-rst` script in civic-v2/client/scripts\n' +
iconData.icons.map((icon) => mustache.render(template, icon)).join('\n')

// Define the path to the output RST file
const outputPath = path.join(
__dirname,
'..',
'src',
'app',
'generated',
'civic-docs.aliases.rst'
)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})

async function promptUser(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
const lowercasedAnswer = answer.toLowerCase()
if (['y', 'yes', ''].includes(lowercasedAnswer)) {
resolve(true)
} else if (['n', 'no'].includes(lowercasedAnswer)) {
resolve(false)
} else {
console.log(
"Invalid response. Please answer with 'y', 'yes', 'n', or 'no'."
)
resolve(promptUser(question)) // re-prompt if invalid response
}
})
})
}

// Write the rendered RST to the file
fs.writeFileSync(outputPath, renderedRst)
;(async function () {
if (fs.existsSync(docsRepoPath)) {
if (fs.existsSync(targetDirectory)) {
if (fs.existsSync(targetFile)) {
const answer = await promptUser(
'civic-docs/generated/civic-docs.aliases.rst exists, overwrite? (Y/n) '
)
if (answer) {
fs.writeFileSync(targetFile, renderedRst)
console.log(`Overwrote ${targetFile}`)
} else {
console.log('Did not overwrite the file.')
}
} else {
fs.writeFileSync(targetFile, renderedRst)
console.log(`Wrote to ${targetFile}`)
}
} else {
const answer = await promptUser(
'Create civic-docs/generated directory and write civic-docs.aliases.rst file? (Y/n) '
)
if (answer) {
fs.mkdirSync(targetDirectory, { recursive: true })
fs.writeFileSync(targetFile, renderedRst)
console.log(`Created directory and wrote to ${targetFile}`)
} else {
console.log('Did not create the directory or write the file.')
}
}
} else {
console.log(
`No civic-docs repository found at ${docsRepoPath}. Will not write civic-docs.aliases.rst file.`
)
}

console.log(`RST written to ${outputPath}`)
rl.close()
})()
4 changes: 2 additions & 2 deletions client/src/app/generated/civic-docs.aliases.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
..
GENERATED BY CiVIC v2 CLIENT SCRIPT, DO NOT EDIT
GENERATED BY CiVIC CLIENT DEV SCRIPT, DO NOT EDIT
(unless you know what you are doing)
Produced by `generate-icon-rst` script in civic-v2/client project
Produced by `generate-icon-rst` script in civic-v2/client/scripts
.. |attribute-adverseresponse| image:: /images/icons/attribute/adverseresponse-outline.svg
:class: 'cvc-icon'
Expand Down

0 comments on commit b7447bc

Please sign in to comment.