File size: 2,396 Bytes
6582e08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*!
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

import * as child_process from 'child_process' // eslint-disable-line no-restricted-imports
import * as nodefs from 'fs' // eslint-disable-line no-restricted-imports
import { marked } from 'marked'
import * as path from 'path'

// doesn't use path utils as this should be formatted for finding images with HTML markup
const projectRoot = process.cwd()

/**
 * replaces relative paths with an `!!EXTENSIONROOT!!` token.
 * This makes it easier to swap in relative links when the extension loads.
 * @param root Repository root
 * @param inputFile Input .md file to swap to HTML
 * @param outputFile Filepath to output HTML to
 * @param cn Converts "AWS" to "Amazon" for CN-based compute.
 */
function translateReadmeToHtml(
    root: string,
    inputFile: string,
    outputFile: string,
    throwIfNotExists: boolean,
    cn: boolean = false
) {
    const inputPath = path.join(root, inputFile)
    if (!nodefs.existsSync(inputPath)) {
        if (throwIfNotExists) {
            throw Error(`File ${inputFile} was not found, but it is required.`)
        }
        console.log(`File ${inputFile} was not found, skipping transformation...`)
        return
    }
    const fileText = nodefs.readFileSync(path.join(root, inputFile)).toString()
    const relativePathRegex = /]\(\.\//g
    let transformedText = fileText.replace(relativePathRegex, '](!!EXTENSIONROOT!!/')
    if (cn) {
        transformedText = transformedText.replace(/AWS/g, 'Amazon').replace(/-en.png/g, '-cn.png')
    }

    const r = marked.parse(transformedText, { async: false })
    if (typeof r !== 'string') {
        throw Error()
    }
    nodefs.writeFileSync(path.join(root, outputFile), r)
}

/**
 * Do a best effort job of generating a git hash and putting it into the package
 */
function generateFileHash(root: string) {
    try {
        const response = child_process.execSync('git rev-parse HEAD')
        nodefs.mkdirSync(root, { recursive: true })
        nodefs.writeFileSync(path.join(root, '.gitcommit'), response)
    } catch (e) {
        console.log(`Getting commit hash failed ${e}`)
    }
}

try {
    translateReadmeToHtml(projectRoot, 'README.md', 'quickStartVscode.html', true)
    generateFileHash(projectRoot)
} catch (error) {
    console.error(error)
    process.exit(100)
}