登录
首页 >  Golang >  Go问答

如何将package.txt中的内容添加到package.json文件的脚本对象中?

来源:stackoverflow

时间:2024-02-13 13:36:23 208浏览 收藏

哈喽!今天心血来潮给大家带来了《如何将package.txt中的内容添加到package.json文件的脚本对象中?》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我的 package.txt 包含:-

"scripts": {
    "lint": "eslint --fix .",
    "lint-check": "eslint .",
  }

我正在使用 npx create-react-app --template typescript 创建一个 react 应用程序。一旦我的 react 应用程序在项目名称下创建,它将具有 package.json 文件。现在我已将 package.txt 文件存储在另一个目录中,并且我想将 package.txt 的脚本与项目的 package.jsonpackage.json 合并,而不会丢失项目的脚本详细信息。简而言之,它应该只在 package.txt 的脚本部分附加项目的 package.json 。

我正在使用 golang 并且我尝试了几种方法:-

func demo() {
    projDir := filepath.Join(react.ProjectName, "package.json")
    f, err := os.OpenFile(projDir, os.O_APPEND|os.O_WRONLY, 0644)
    if err != nil {
        log.Println(err)
    }
    defer f.Close()
    if _, err := f.WriteString(packageJSON); err != nil {
        log.Println(err)
    }
    
}

它在项目的 package.json 的最后添加,但没有添加 package.json 的脚本部分。请帮助我


正确答案


假设您的 package.txt 文件包含:

"scripts": {
    "lint": "eslint --fix .",
    "lint-check": "eslint .",
}

package.json 文件包含:

{
    "some": "other fields",
    "scripts": {
        "start": "node index.js",
        "dev": "nodemon"
    },
    "another": "other fields"
}

如果您需要您提到的工作,请手动完成。

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "strings"
)

func main() {
    demo()
}

func demo() {
    // getting .txt file content
    txtbyte, err := ioutil.readfile("./package.txt")
    if err != nil {
        log.fatal(err)
    }
    txtstring := string(txtbyte)

    // getting main lines to append
    txtlines := getmainlines(txtstring)

    // getting .json file content
    jsonbyte, err := ioutil.readfile("./package.json")
    if err != nil {
        log.fatal(err)
    }
    jsonstring := string(jsonbyte)

    // getting index of where the main lines will be append on .json file
    indexofappend := getindex(jsonstring, `"scripts": {`)

    // merging
    finalstring := merge(jsonstring, indexofappend, txtlines)

    fmt.println(finalstring)
}

func getmainlines(txtstring string) string {
    // eleminating `"scripts": {` part from .txt file
    txtstring = strings.trimspace(strings.trimprefix(txtstring, `"scripts": {`))

    // eleminating `}` part from .txt file
    txtstring = strings.trimspace(strings.trimsuffix(txtstring, `}`))

    // finally return main lines
    return txtstring
}

func getindex(jsonstring, substr string) int {
    // identifying index of where to append
    index := strings.index(jsonstring, substr)
    if index == -1 {
        fmt.println("invalid sub-string")
    }

    // index is the value of first char of substr
    // so adding the length upto `{`
    return index + 12 //  `"scripts": {` -->  have 12 chars
}

func merge(jsonstring string, indexofappend int, txtlines string) string {
    // newstr is consist of 3 part
    // first: content of .json file upto `"scripts": {`
    // second: content of main lines
    // thirs: last content of .json file
    newstr := jsonstring[:indexofappend] + txtlines + jsonstring[indexofappend:]

    return newstr
}

相应地处理空格、制表符、逗号和其他内容。

最后将最终结果/字符串写入现有的 package.json 文件:

    // now write the final result to package.json file
    err = ioutil.WriteFile("./package.json", []byte(finalString), 0644)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("successfully merged")

本篇关于《如何将package.txt中的内容添加到package.json文件的脚本对象中?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>