@@ -5,12 +5,13 @@ import { LockMessage } from "./types";
55import * as path from "path" ;
66import * as fs from "fs" ;
77import { glob } from "glob" ;
8+ import * as mm from "micromatch" ;
89
910export function registerCommands ( ctx :vscode . ExtensionContext ) {
1011 const ctrl = new Controller ( ctx ) ;
1112 ctx . subscriptions . push ( ctrl ) ;
1213
13- function indicatorAction ( ) {
14+ function toggleLock ( ) {
1415 ctrl . toggleLock ( ) ;
1516 }
1617
@@ -32,63 +33,62 @@ export function registerCommands(ctx:vscode.ExtensionContext) {
3233
3334 function ctxOpen ( msg :LockMessage ) {
3435 vscode . workspace . workspaceFolders ?. forEach ( ( ws ) => {
35- const [ , fileName ] = msg . file . split ( ":" ) ;
36- const file = path . join ( ws . uri . fsPath , fileName ) ;
37- console . log ( "Try open" , file ) ;
38- if ( fs . existsSync ( file ) ) {
39- vscode . window . showTextDocument ( vscode . Uri . parse ( file ) ) ;
40- return ;
41- }
36+ const [ ns , fileName ] = msg . file . split ( ":" ) ;
37+ const mask = path . join ( ws . uri . path , '**' , ns , "**" , fileName ) ;
38+ glob ( mask )
39+ . then ( ( files ) => {
40+ if ( files ?. length ) {
41+ const [ file ] = files ;
42+ vscode . window . showTextDocument ( vscode . Uri . parse ( file ) ) ;
43+ } else {
44+ vscode . window . showErrorMessage ( `Cannot open document,using ${ mask } ` ) ;
45+ }
46+ } ) ;
4247 } ) ;
4348 }
4449
4550 function ctxLockFolder ( startDir :vscode . Uri ) {
46- // eslint-disable-next-line @typescript-eslint/no-unused-vars
47- const lockTree = ( uri :vscode . Uri , ign ?:string [ ] ) => {
48- vscode . workspace . fs . readDirectory ( uri )
49- . then ( ( dirs ) => {
50- dirs . forEach ( ( [ file , type ] ) => {
51- const lock = vscode . Uri . parse ( path . join ( uri . path , file ) ) ;
52- if ( type === vscode . FileType . Directory ) {
53- if ( ign ) {
54- lockTree ( lock , ign ) ;
55- }
56- } else {
57- console . log ( "Loking" , lock . path ) ;
58- }
59- } ) ;
60- } ) ;
61- } ;
6251
6352 // eslint-disable-next-line @typescript-eslint/no-unused-vars
6453 const getFiles = ( ignore :string [ ] ) => {
65- glob ( path . join ( startDir . path , '**/*' ) , { ignore, dot :true } )
54+ return glob ( path . join ( startDir . path , '**/*' ) , { ignore, dot :true } )
6655 . then ( ( files ) => {
67- console . log ( "Files" , files ) ;
56+ return mm ( files , "**/*.*" , { ignore } ) ;
6857 } ) ;
6958 } ;
7059
60+ const getIgnores = async ( ) => {
61+ const root = vscode . workspace . getWorkspaceFolder ( startDir ) ;
62+ const mask = path . join ( root ! . uri . path , "**" , ".gitignore" ) ;
63+ return glob ( mask )
64+ . then ( ( files ) => {
65+ return files . map ( ( gitIgnore ) => {
66+ const content = fs . readFileSync ( gitIgnore ) . toString ( "utf8" ) ;
67+ return content . split ( "\n" ) . map ( ( line ) => line . trim ( ) )
68+ . filter ( line => line . length > 0 && ! line . startsWith ( "#" ) ) ;
69+ } ) ;
70+ } ) . then ( ( igns ) => {
71+ const uniq = new Set ( ) ;
72+ igns . forEach ( ( file ) => file . map ( f => uniq . add ( `*/**/${ f } ` ) ) ) ;
73+ return Array . from ( uniq . keys ( ) ) as string [ ] ;
74+ } ) ;
75+ } ;
7176
72- const root = vscode . workspace . getWorkspaceFolder ( startDir ) ;
73- const gitIgnore = path . join ( root ! . uri . path , ".gitignore" ) ;
74- if ( fs . existsSync ( gitIgnore ) ) {
75- const content = fs . readFileSync ( gitIgnore ) . toString ( "utf8" ) ;
76- const ignores = content . split ( "\n" ) . map ( ( line ) => line . trim ( ) ) . filter ( line => line . length > 0 && ! line . startsWith ( "#" ) ) ;
77- console . log ( "IGN" , ignores . map ( ( f ) => path . join ( "**" , f ) ) ) ;
78- // getFiles(ignores);
79- // lockTree(startDir,ignores);
80- } else {
81- vscode . window . showInformationMessage ( "Where are no .gitignore file, only upper level will be processed..." ) ;
82- // lockTree(startDir);
83- }
84- // console.log("FILE",gitIgnore);
85- // lockTree(startDir);
86- // vscode.Uri.parse(path.join(uri.path,file)
87- //lockTree(startDir);
77+ getIgnores ( )
78+ . then ( ( ignores ) => {
79+ console . log ( "IGNO" , ignores ) ;
80+ getFiles ( ignores )
81+ . then ( ( locks ) => {
82+ ctrl . storage . lockGroup ( locks ) ;
83+ } ) ;
84+ } ) ;
8885 }
8986
9087
91- ctx . subscriptions . push ( vscode . commands . registerCommand ( C . statusBarAction , indicatorAction ) ) ;
88+ /*
89+ Status bar toggler
90+ */
91+ ctx . subscriptions . push ( vscode . commands . registerCommand ( C . statusBarAction , toggleLock ) ) ;
9292
9393 /*
9494 Reflects other side locks
@@ -102,9 +102,13 @@ export function registerCommands(ctx:vscode.ExtensionContext) {
102102 ctx . subscriptions . push ( vscode . commands . registerCommand ( C . LockCommands . unlock , ( ) => updateLock ( C . LockState . Unlocked ) ) ) ;
103103 ctx . subscriptions . push ( vscode . commands . registerCommand ( C . LockCommands . wipeLocked , ( ) => wipeLocked ( ) ) ) ;
104104 ctx . subscriptions . push ( vscode . commands . registerCommand ( C . LockCommands . ctxUnlock , ( args ) => ctxUnlock ( args ) ) ) ;
105- ctx . subscriptions . push ( vscode . commands . registerCommand ( C . LockCommands . ctxOpen , ( args ) => ctxOpen ( args ) ) ) ;
106105 ctx . subscriptions . push ( vscode . commands . registerCommand ( C . LockCommands . lockFolder , ( args ) => ctxLockFolder ( args ) ) ) ;
107106
107+ /*
108+ Open doc from expoloer
109+ */
110+ ctx . subscriptions . push ( vscode . commands . registerCommand ( C . LockCommands . ctxOpen , ( args ) => ctxOpen ( args ) ) ) ;
111+
108112
109113 return ctrl ;
110114}
0 commit comments