'매크로'기록? 또는 Visual Studio Code의 일련의 작업?

Sep 20 2020

나는 이와 같은 것이 필요할 것입니다. 프로젝트 내에 폴더를 생성하면. 예를 들어 React 프로젝트. 해당 폴더를 선택하고이를 수행하는 일련의 매크로 또는 일련의 작업을 실행할 수 있습니다.

Component예를 들어 폴더 이름이있는 경우 매크로가 재생 될 때 다음과 같이됩니다.

Component.js일종의 스 니펫 기본 콘텐츠가 있는 이름으로 파일을 만듭니다 . 이름이있는 파일을 만듭니다.이 파일에는 Component.styled.js일종의 스 니펫 기본 콘텐츠가 포함됩니다. index.jsContent :가 있는 이름으로 파일을 만듭니다 . export { default } from './Component'여기서 Component는 실제로 폴더의 이름입니다.

실제로 어디에서 시작해야할지 모르겠습니다. 매크로를 살펴 봤고 Visual Studio 코드 사용자 스 니펫을 사용하는 방법을 알고 있지만이 작업을 어떻게 수행 할 수 있는지 잘 모르겠습니다.

답변

1 Mark Oct 10 2020 at 02:33

macro-commander 라는 매크로 확장을 사용하면 폴더와 포함 된 파일 (원하는 경우 일부 기본 내용 포함)을 상당히 쉽게 만들 수 있습니다. 그러나 본질적으로 settings.json에 확장을 작성하기 때문에 약간 복잡해 보이지만 작동 중에는 부드럽습니다. 이 코드는 확장을 설치 한 후 settings.json에 들어갑니다.

"macros": {

  "createReactFolderAndFiles" : [
  
    { 
      "javascript": [

        "const newReactFolder = await window.showInputBox()",  // get the component's name

            // create a object to store various edits, including file creation, deletion and edits
        "const we = new vscode.WorkspaceEdit();",

            // get the workspace folder path + file:/// uri scheme
        "const thisWorkspace = vscode.workspace.workspaceFolders[0].uri.toString();",  // file:///c:/Users/Mark/OneDrive/Test Bed
            // append the new folder name
        "const uriBase = `${thisWorkspace}/${newReactFolder}`;",  // file:///c:/Users/Mark/OneDrive/Test Bed/Test

        // "await window.showInformationMessage(` ${uriBase}`)", // just handy to check on progress // create uri's for the three files to be created "let newUri1 = vscode.Uri.parse(`${uriBase}/index.js`);",
        "let newUri2 = vscode.Uri.parse(`${uriBase}/${newReactFolder}.js`);",
        "let newUri3 = vscode.Uri.parse(`${uriBase}/${newReactFolder}.styled.js`);",

        "let newFiles = [ newUri1, newUri2, newUri3];",

        "for (const newFile of newFiles) { let document = await we.createFile(newFile, { ignoreIfExists: false, overwrite: true });};",

        "let styledContent = `first line flush left",   // how to use template literals in this macro
                             "second line will be flush left",
                             "   third line indented`;",

              //  insert text into the top of each file
        "await we.insert(newUri1, new vscode.Position(0, 0), `export { default } from './${newReactFolder}'`);", "await we.insert(newUri2, new vscode.Position(0, 0), `my ${newReactFolder}.js content`);",
        "await we.insert(newUri3, new vscode.Position(0, 0), styledContent);",
        
        "await vscode.workspace.applyEdit(we);",  // apply all the edits: file creation and adding text

        "for (const newFile of newFiles) { let document = await vscode.workspace.openTextDocument(newFile); await document.save(); };",
      ]
    }
  ]
},

설정에서 vscode 확장 API를 사용할 수 있습니다. 이 매크로를 실행하려면 키 바인딩을 설정하십시오.

{
  "key": "alt+j",
  "command": "macros.createReactFolderAndFiles"
},

가장 먼저 할 일은 폴더 이름에 대한 입력 상자를 팝업하는 것입니다. 하나를 입력하고를 누르십시오 Enter.


반복적으로 사용하려는 기본 콘텐츠가있는 경우이를 저장하고이 매크로에서 사용하기 위해 검색 할 수 있습니다. 기본 콘텐츠가 동일한 작업 공간에 있다고 가정합니다 /templates/defaultComponent.txt.

그러면 해당 콘텐츠가 표시됩니다.

  "let defaultContent = await vscode.workspace.fs.readFile(vscode.Uri.parse(`${thisWorkspace}/templates/defaultComponent.txt`));",

파일 중 하나에 추가하십시오.

"await we.insert(newUri3, new vscode.Position(0, 0), defaultContent.toString());",

데모에서 볼 수 있듯이 어떤 이유로 든 새로 생성 된 파일 몇 개가 항상 열립니다.이를 방지하는 방법을 모르겠습니다.

코드가 복잡해 보이지만 다른 파일 이름이나 내용에 맞게 수정하는 것이 간단하다고 생각합니다.