<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="https://www.fenzland.com/en/feed.xml" rel="self" type="application/atom+xml" /><link href="https://www.fenzland.com/en/" rel="alternate" type="text/html" hreflang="en" /><updated>2019-02-19T16:57:48+08:00</updated><id>https://www.fenzland.com/en/feed.xml</id><title type="html">Fenzland</title><subtitle>瘋狂地追逐，然後靜下來沈澱。 勿讓青春在不經意間消逝。</subtitle><entry><title type="html">Promisify of NodeJS async API</title><link href="https://www.fenzland.com/en/coding/js/promisify-plan-for-node-api" rel="alternate" type="text/html" title="Promisify of NodeJS async API" /><published>2018-12-30T15:38:06+08:00</published><updated>2018-12-30T15:38:06+08:00</updated><id>https://www.fenzland.com/en/coding/js/promisify-plan-for-node-api</id><content type="html" xml:base="https://www.fenzland.com/en/coding/js/promisify-plan-for-node-api">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;　　
Promise is the core of the modern async programming. 
But the NodeJS is designed before the Promise. 
So its huge number of async APIs are base on callback. 
We cannot use them unless we change them promisive, or say promisify. 
A ideal way is ask Ryan Dahl to change the APIs. 
Returns a promise instead of throw an error when the callback is not given. 
But it’s bad to take him from focus on deno. So, we must do it ourselves.&lt;/p&gt;

&lt;p&gt;　　
For the huge number, wrap them one by one is not a good plan. 
Use what, wrap what is not the best too. 
We need a general, and as light-weight as better way to make this.&lt;/p&gt;

&lt;p&gt;　　
So I make this:&lt;/p&gt;

&lt;h2 id=&quot;declare&quot;&gt;Declare&lt;/h2&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;reject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
					&lt;span class=&quot;nx&quot;&gt;reject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
					&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Promisify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Proxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;receiver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Reflect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;has&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
					&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
				
				&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
					&lt;span class=&quot;nx&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
						&lt;span class=&quot;nb&quot;&gt;Reflect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;receiver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
						&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
					&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
				&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;usage&quot;&gt;Usage&lt;/h2&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Do&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'fs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;


&lt;span class=&quot;nx&quot;&gt;Do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path/to/file'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'utf-8'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readdir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path/to/directory'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;// Promisify&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pfs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Promisify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;


&lt;span class=&quot;nx&quot;&gt;pfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path/to/file'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'utf-8'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readdir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path/to/directory'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;designing-concept&quot;&gt;Designing Concept&lt;/h2&gt;
&lt;p&gt;　　
The async APIs of NodeJS follows a standard: no matter how many the parameters, 
the last parameter is the callback. And there are two parameters for callback. 
The first is the error or null, the second is the result. 
So the function “Do” designed.
　　
But the function “Do”, is not so comfortable for JS programmers. 
If we can access new APIs just like the old one, it’s better. 
For this purpose, we can use Proxy to intercept the accessing, 
and return the promisified API by the Do function. 
At the last, we cache the promisified API to avoid &lt;code class=&quot;highlighter-rouge&quot;&gt;pfs.readFile !== pfs.readFile&lt;/code&gt;.&lt;/p&gt;</content><author><name></name></author><summary type="html">　　 Promise is the core of the modern async programming. But the NodeJS is designed before the Promise. So it's huge number of async APIs are base on callback. We cannot use them unless we change them promisive, or say promisify. For the huge number, wrap them one by one is not a good plan. We need a general, and as light-weight as better way to make this.</summary></entry><entry><title type="html">Using Git Index</title><link href="https://www.fenzland.com/en/coding/git/git-index" rel="alternate" type="text/html" title="Using Git Index" /><published>2018-12-16T13:26:21+08:00</published><updated>2018-12-16T13:26:21+08:00</updated><id>https://www.fenzland.com/en/coding/git/git-index</id><content type="html" xml:base="https://www.fenzland.com/en/coding/git/git-index">&lt;p&gt;　　
An healthy git repository, is readable and tracable. 
We need fully clear what we commit. 
The Git Index, is what we need, just like a shopping cart. 
We pick goods(updates) from the shelves(work tree). 
Then push the cart(index) to cash register, and buy(commit) them.
With the Git Index, we have ability to deal with a big heap of updates orderly.&lt;/p&gt;

&lt;h4 id=&quot;basic-commands&quot;&gt;Basic Commands&lt;/h4&gt;
&lt;p&gt;　　
There are three git commands for managing Git Index. 
They are &lt;code class=&quot;highlighter-rouge&quot;&gt;add&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;reset&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;checkout&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;add&lt;/code&gt;      add updates from work tree to index&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;reset&lt;/code&gt;    remove updates from index&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;checkout&lt;/code&gt; give up updates that not be added to index (cover updates in work tree by index)
　　
Both &lt;code class=&quot;highlighter-rouge&quot;&gt;reset&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;checkout&lt;/code&gt; are multi-purpose. 
The command &lt;code class=&quot;highlighter-rouge&quot;&gt;reset&lt;/code&gt; also can use to move current branch. 
And the command &lt;code class=&quot;highlighter-rouge&quot;&gt;checkout&lt;/code&gt; also can use to move HEAD and checkout to the work tree. 
The difference is the parameter for the command. 
If the parameter is a commit(hash or branch, tag…), it’s for the branch and HEAD. 
If the parameter is a path, or there is no parameter, it’s for the index.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;meticulous-operations---patch&quot;&gt;Meticulous operations: &lt;code class=&quot;highlighter-rouge&quot;&gt;--patch&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;　　
Each of &lt;code class=&quot;highlighter-rouge&quot;&gt;add&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;reset&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;checkout&lt;/code&gt; can run with option &lt;code class=&quot;highlighter-rouge&quot;&gt;--patch&lt;/code&gt;, or &lt;code class=&quot;highlighter-rouge&quot;&gt;-p&lt;/code&gt; in short. 
Git will iterate over the updates and let’s handle them one by one.
You can run with paths as parameters to iterate over one file or several files, 
or just run with no parameter to iterate over all updates. 
Using there command with &lt;code class=&quot;highlighter-rouge&quot;&gt;-p&lt;/code&gt; option is the best way currently to manage the Git Index. 
I name it &lt;em&gt;3Ps Sorcery&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;　　
After run the command, we get a interactive interface like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt;&lt;span class=&quot;gh&quot;&gt;diff --git a/index.md b/index.md
index 921ba51..1c0d98b 100644
&lt;/span&gt;&lt;span class=&quot;gd&quot;&gt;--- a/index.md
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+++ b/index.md
&lt;/span&gt;&lt;span class=&quot;gu&quot;&gt;@@ -3,5 +3,5 @@
&lt;/span&gt; 
 
 layout: home
&lt;span class=&quot;gd&quot;&gt;-list_title: foo
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+list_title: bar
&lt;/span&gt; ---
Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,?]? &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The last Line is different with different command:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt;add
Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,?]? 

reset
Unstage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,?]? 

checkout
Discard this hunk from worktree [y,n,q,a,d,k,K,j,J,g,/,s,e,?]? &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
As we see, git show an update in the file &lt;code class=&quot;highlighter-rouge&quot;&gt;index.md&lt;/code&gt;. 
And give us several operations &lt;code class=&quot;highlighter-rouge&quot;&gt;y,n,q,a,d,k,K,j,J,g,/,s,e,?&lt;/code&gt;. What are they use for? 
By the first sight, I only know the &lt;code class=&quot;highlighter-rouge&quot;&gt;?&lt;/code&gt; provides helps. 
So we input &lt;kbd&gt;?&lt;/kbd&gt; &lt;kbd&gt;Enter&lt;/kbd&gt;.
We get the message:&lt;/p&gt;

&lt;h5 id=&quot;add&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;add&lt;/code&gt;&lt;/h5&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h5 id=&quot;reset&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;reset&lt;/code&gt;&lt;/h5&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;y - unstage this hunk
n - do not unstage this hunk
q - quit; do not unstage this hunk or any of the remaining ones
a - unstage this hunk and all later hunks in the file
d - do not unstage this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h5 id=&quot;checkout&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;checkout&lt;/code&gt;&lt;/h5&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;y - discard this hunk from worktree
n - do not discard this hunk from worktree
q - quit; do not discard this hunk or any of the remaining ones
a - discard this hunk and all later hunks in the file
d - do not discard this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
What’s a hugging heap of dazing stuffs! 
Don’t worry, just let’s reorder them:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;handlers:
`y` yes       do (add/reset/give up);
`n` no        don't (add/reset/give up);
`a` all       do    for all in this file;
`d` don't     don't for all in this file;
`e` edit      see next section;
`s` split     some times, git will assume several updates as one, you can split it;

navigations (only works in a file):
`g` goto      goto the nth update, such as `g 1`，`g 2`, the number start with 1;
`/` search    just like searching in vim, less... It's convenient for a big file with lots of updates;
`j` next      next unprocessed;
`J` Next      next update;
`k` previous  previous unprocessed;
`K` Previous  previous update;

others:
`q` quit      quit；
`?` help      print help；&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
We only need know &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;n&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;s&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;q&lt;/code&gt; for basic using. 
For advanced using, we need know one more: &lt;code class=&quot;highlighter-rouge&quot;&gt;e&lt;/code&gt;.
For programmers, that’s enough. 
For dictionary keeper, or Webpack configuratician, navigations will be useful.&lt;/p&gt;

&lt;p&gt;　　
In this section, we talk about the &lt;code class=&quot;highlighter-rouge&quot;&gt;e&lt;/code&gt;. 
To begin the editing, we need an editor. 
Just like when you run &lt;code class=&quot;highlighter-rouge&quot;&gt;commit&lt;/code&gt; without &lt;code class=&quot;highlighter-rouge&quot;&gt;-m&lt;/code&gt; opiton, 
git will open vim or nano when you using &lt;code class=&quot;highlighter-rouge&quot;&gt;e&lt;/code&gt;. 
And the text in the editor will like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt;# Manual hunk edit mode -- see bottom for a quick guide
&lt;span class=&quot;gu&quot;&gt;@@ -20,4 +20,5 @@
&lt;/span&gt; 	&amp;gt;&amp;lt;ul
 		&amp;gt;&amp;lt;li&amp;gt;foo&amp;lt;/li
&lt;span class=&quot;gd&quot;&gt;-		&amp;gt;&amp;lt;li&amp;gt;bar&amp;lt;/li
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+		&amp;gt;&amp;lt;li&amp;gt;baz&amp;lt;/li
+		&amp;gt;&amp;lt;li&amp;gt;qux&amp;lt;/li
&lt;/span&gt; 	&amp;gt;&amp;lt;/ul
# ---
# Here will be some manual text.&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
Here is an update, we delete the line &lt;em&gt;bar&lt;/em&gt;, and insert lines &lt;em&gt;baz&lt;/em&gt; and &lt;em&gt;qux&lt;/em&gt;. 
The line &lt;code class=&quot;highlighter-rouge&quot;&gt;@@ ... @@&lt;/code&gt; shows the line number and line counts before and after edit. 
And follows the content of update. 
The first character of each line is the line type, can be &lt;em&gt;space&lt;/em&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt;. 
The &lt;em&gt;space&lt;/em&gt; means this line is a normal line, 
a &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; for a deleted line, and an &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; for a new inserted line. 
Attention, this &lt;em&gt;space&lt;/em&gt; is not indentation. 
If you use indent characters for indentation, here is a &lt;em&gt;space&lt;/em&gt; at the first. 
If you use spaces instead of indentation, one more &lt;em&gt;space&lt;/em&gt; each line.&lt;/p&gt;

&lt;p&gt;　　
If you just quit the editor without edit anything, 
it’s just equal to press a &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;e&lt;/code&gt;. 
If you recover the update, remove all &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; lines, change &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt;s to &lt;em&gt;space&lt;/em&gt;s, 
it’s just equal to press a &lt;code class=&quot;highlighter-rouge&quot;&gt;n&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;e&lt;/code&gt;. 
We can modify it as you like, but not all modifications are allowed.&lt;/p&gt;

&lt;p&gt;　　
It’s easier to understand when we run &lt;code class=&quot;highlighter-rouge&quot;&gt;add&lt;/code&gt;. Here is an example, 
if we make modifications like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt; 	&amp;gt;&amp;lt;ul
&lt;span class=&quot;gd&quot;&gt;-		&amp;gt;&amp;lt;li&amp;gt;foo&amp;lt;/li
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+		&amp;gt;&amp;lt;li&amp;gt;qux&amp;lt;/li
&lt;/span&gt; 		&amp;gt;&amp;lt;li&amp;gt;bar&amp;lt;/li
&lt;span class=&quot;gi&quot;&gt;+		&amp;gt;&amp;lt;li&amp;gt;Baz&amp;lt;/li
&lt;/span&gt; 	&amp;gt;&amp;lt;/ul&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;blockquote&gt;

  &lt;p&gt;As we see, we don’t delete line &lt;em&gt;bar&lt;/em&gt;, but delete line &lt;em&gt;foo&lt;/em&gt;. 
And we edit line &lt;em&gt;baz&lt;/em&gt;, and insert &lt;em&gt;qux&lt;/em&gt; to another position. 
That’s not difficult. 
We’ll found that &lt;em&gt;space&lt;/em&gt; lines and &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; lines can change to each others, but you can not modify their content; 
&lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; lines can make any modifications, remove or insert anywhere when &lt;code class=&quot;highlighter-rouge&quot;&gt;add&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;　　
But when &lt;code class=&quot;highlighter-rouge&quot;&gt;reset&lt;/code&gt;, it’s different. Oppositely, &lt;em&gt;space&lt;/em&gt; lines and &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; lines can change to each others, 
but you can not modify their content; &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; lines can make any modifications, remove or insert anywhere when &lt;code class=&quot;highlighter-rouge&quot;&gt;add&lt;/code&gt;. 
It’s confusing. So, example again. the same situation, let’s edit the text to this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt; 	&amp;gt;&amp;lt;ul
&lt;span class=&quot;gi&quot;&gt;+		&amp;gt;&amp;lt;li&amp;gt;foo&amp;lt;/li
&lt;/span&gt;&lt;span class=&quot;gd&quot;&gt;-		&amp;gt;&amp;lt;li&amp;gt;Bar&amp;lt;/li
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+		&amp;gt;&amp;lt;li&amp;gt;baz&amp;lt;/li
&lt;/span&gt; 		&amp;gt;&amp;lt;li&amp;gt;qux&amp;lt;/li
&lt;span class=&quot;gd&quot;&gt;-		&amp;gt;&amp;lt;li&amp;gt;ET&amp;lt;/li
&lt;/span&gt; 	&amp;gt;&amp;lt;/ul&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To understand what will happen, we must using words like &lt;em&gt;give up&lt;/em&gt; or &lt;em&gt;cancel&lt;/em&gt;. 
We change line &lt;em&gt;foo&lt;/em&gt; from &lt;em&gt;space&lt;/em&gt; line to &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; line, means we cancel the adding of line &lt;em&gt;foo&lt;/em&gt;, 
but the line &lt;em&gt;foo&lt;/em&gt; is not added, it’s over there original, 
so it’s means delete the line &lt;em&gt;foo&lt;/em&gt; in fact. 
We change line &lt;em&gt;bar&lt;/em&gt; to line &lt;em&gt;Bar&lt;/em&gt;, means don’t cancel the deleting of &lt;em&gt;bar&lt;/em&gt;, 
but cancel deleting of line &lt;em&gt;Bar&lt;/em&gt;. And means delete line &lt;em&gt;bar&lt;/em&gt; and add line &lt;em&gt;Bar&lt;/em&gt; in fact. 
At last, insert a line &lt;em&gt;ET&lt;/em&gt;. 
If you still hard to get it, just assume &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; lines as &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; lines and &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; lines as &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; lines.&lt;/p&gt;

&lt;p&gt;　　
On &lt;code class=&quot;highlighter-rouge&quot;&gt;checkout&lt;/code&gt;, since the direction is same with &lt;code class=&quot;highlighter-rouge&quot;&gt;reset&lt;/code&gt;, so reverse &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; too. 
What in the paper is abstract. For fully understand, you need try it by your self.&lt;/p&gt;

&lt;p&gt;　　
There is a feature lack that, the &lt;code class=&quot;highlighter-rouge&quot;&gt;add -p&lt;/code&gt; doesn’t with new files. However, &lt;code class=&quot;highlighter-rouge&quot;&gt;reset -p&lt;/code&gt; dose. 
For the new files, you can use &lt;code class=&quot;highlighter-rouge&quot;&gt;add filename&lt;/code&gt; then &lt;code class=&quot;highlighter-rouge&quot;&gt;reset -p filename&lt;/code&gt; to add it with patching.&lt;/p&gt;

&lt;h4 id=&quot;file-level-and-global-operations&quot;&gt;File level and global operations&lt;/h4&gt;
&lt;p&gt;　　
Usually, always check over what you change with &lt;code class=&quot;highlighter-rouge&quot;&gt;-p&lt;/code&gt; is fine. 
But some times you need file level or global operations.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git add file&lt;/code&gt;      add all updates in the file from work tree to index&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git reset file&lt;/code&gt;    remove all updates in the file from index&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git checkout file&lt;/code&gt; give up all updates in the file that not be added to index (cover the file in work tree by index)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git add .&lt;/code&gt;    add all updates from work tree to index&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git reset&lt;/code&gt;    remove all updates from index&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git checkout&lt;/code&gt; give up all updates that not be added to index (cover all updates in work tree by index)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;imperfect-ultimate-weapon-add---interactive&quot;&gt;Imperfect ultimate weapon &lt;code class=&quot;highlighter-rouge&quot;&gt;add --interactive&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;　　
There is another more fashion option &lt;code class=&quot;highlighter-rouge&quot;&gt;--interactive&lt;/code&gt; on command &lt;code class=&quot;highlighter-rouge&quot;&gt;git add&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;-i&lt;/code&gt; in short. 
By the name, this is a interactive interface too. It seems like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:    unchanged        +1/-1 index.html

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now&amp;gt; &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It’s shows &lt;code class=&quot;highlighter-rouge&quot;&gt;status&lt;/code&gt; above which like &lt;code class=&quot;highlighter-rouge&quot;&gt;git status&lt;/code&gt; but more clear, and commands below. 
There are 8 commands shown, and a &lt;code class=&quot;highlighter-rouge&quot;&gt;?&lt;/code&gt; hidden:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;1 status         show the main interface, usually for refresh
2 update         add files into index, just like `add file` but not work for new files
3 revert         remove files from index，just like `reset file`
4 add untracked  add new files into index
5 patch          like `add -p file`
6 diff           show what in the index
7 quit           quit
8 help           help, about commands
?                help, about the interface&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You can just use the index or the first letter instead of full command.&lt;/p&gt;

&lt;p&gt;　　
There are 5 core commands: &lt;code class=&quot;highlighter-rouge&quot;&gt;u&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;r&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;a&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;p&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;d&lt;/code&gt;. Each of them leads to a second level interface，
the command prompt change from &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&amp;gt;&lt;/code&gt;. 
In this level, we can choose files with index or unique prefix of filename. 
Currently, it’s not support segmental matching, fuzzy matching or &lt;code class=&quot;highlighter-rouge&quot;&gt;*&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;?&lt;/code&gt; yet. 
Nor &lt;kbd&gt;tab&lt;/kbd&gt; hints and input history. Wish they will be added in the future. 
For now, the index is enough. We can add a &lt;code class=&quot;highlighter-rouge&quot;&gt;-&lt;/code&gt; before the index or file path, to remove adding, 
Type &lt;kbd&gt;Enter&lt;/kbd&gt; to confirm the selection and type &lt;kbd&gt;Enter&lt;/kbd&gt; again to take the action.&lt;/p&gt;

&lt;p&gt;　　
The &lt;code class=&quot;highlighter-rouge&quot;&gt;add --i&lt;/code&gt; bring us a great interface, we can play fluently, without repeating &lt;code class=&quot;highlighter-rouge&quot;&gt;git status&lt;/code&gt;. 
On the mode, this is the ultimate weapon. But it’s an imperfect weapon. 
There are &lt;code class=&quot;highlighter-rouge&quot;&gt;add file&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;reset file&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;add -p file&lt;/code&gt; within the &lt;code class=&quot;highlighter-rouge&quot;&gt;add -i&lt;/code&gt;. 
But &lt;code class=&quot;highlighter-rouge&quot;&gt;reset -p file&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;checkout file&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;checkout -p file&lt;/code&gt; are missing. 
So, for right now the best way is still the &lt;em&gt;3Ps Sorcery&lt;/em&gt;.&lt;/p&gt;</content><author><name></name></author><summary type="html">　　 Don't use `git add .` or `git add -A` anymore. There are many of better CLI that git provider for us. Just like a suit of surgical kit. We can use them to manage the Git Index, fine to each tiny change.</summary></entry><entry><title type="html">Git post-update for auto-deploy</title><link href="https://www.fenzland.com/en/coding/git/git-post-update" rel="alternate" type="text/html" title="Git post-update for auto-deploy" /><published>2018-12-01T17:00:00+08:00</published><updated>2018-12-01T17:00:00+08:00</updated><id>https://www.fenzland.com/en/coding/git/git-post-update</id><content type="html" xml:base="https://www.fenzland.com/en/coding/git/git-post-update">&lt;p&gt;　　&lt;/p&gt;

&lt;p&gt;For web project or other server side project, we need auto-deploy. 
For this, we need run some scripts after we push the git commits. 
That’s why post-update here.&lt;/p&gt;

&lt;p&gt;　　
If we put script file under &lt;code class=&quot;highlighter-rouge&quot;&gt;git-dir/hooks/&lt;/code&gt; and name it with &lt;code class=&quot;highlighter-rouge&quot;&gt;post-update&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;chmod -x&lt;/code&gt; for it. 
The file will run after we push some commits to the git repository. 
You can yous any scripting language you like, such as bush, python, ruby, julia, PHP, Node.js…
Compiled as well of cause, but not as easy to read and edit as scripting languages.&lt;/p&gt;

&lt;h3 id=&quot;example-0&quot;&gt;#Example 0.&lt;/h3&gt;
&lt;p&gt;　　
For simple scripts, bash is convenient.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
git &lt;span class=&quot;nt&quot;&gt;--work-tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/path-to/project-dir/ checkout &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;git rev-list &lt;span class=&quot;nt&quot;&gt;--tags&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--max-count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;/dev/null&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This script will checkout the newest tag to the work directory. 
It’s frequently-used.&lt;/p&gt;

&lt;h3 id=&quot;example-1&quot;&gt;#Example 1.&lt;/h3&gt;
&lt;p&gt;　　
This is an example of Laravel project, so we using PHP.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;#!/usr/bin/env php
&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$product_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path-to/project-dir/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$test_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path-to/test-dir/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'refs/heads/migrate'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`git --work-tree=$test_dir checkout -f migrate`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`$test_dir/artisan migrate:fresh --seed`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'refs/heads/test'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`git --work-tree=$test_dir checkout -f test`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`composer --working-dir=$product_dir dump-autoload`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`$product_dir/artisan migrate`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'refs/heads/composer'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`git --work-tree=$test_dir checkout -f composer`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`composer --working-dir=$test_dir update`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`$test_dir/artisan vendor:publish --all`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`git --work-tree=$test_dir checkout -f test`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`composer --working-dir=$product_dir dump-autoload`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`$product_dir/artisan migrate`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		
		&lt;span class=&quot;sb&quot;&gt;`git --work-tree=$product_dir checkout -f composer`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`composer --working-dir=$product_dir update`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`$product_dir/artisan vendor:publish --all`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'refs/heads/product'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`git --work-tree=$product_dir checkout -f &quot;\`git rev-list --tags --max-count=1\`&quot;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`composer --working-dir=$product_dir dump-autoload`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`$product_dir/artisan migrate`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`$product_dir/artisan view:clear`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;sb&quot;&gt;`$product_dir/artisan route:cache`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
This time, we do different things according to different bushed branch. 
When &lt;em&gt;test&lt;/em&gt; branch is pushed, we checkout it to the test directory, and apply the migrations of DB. 
When &lt;em&gt;migrate&lt;/em&gt; branch is pushed, we refresh the DB of the test environment, 
When &lt;em&gt;project&lt;/em&gt; branch is pushed, we checkout the newest tag to the project environment, 
then apply migrations and clear view cache and rebuild route cache.&lt;/p&gt;

&lt;h3 id=&quot;example-2&quot;&gt;#Example 2.&lt;/h3&gt;
&lt;p&gt;　　
Here is the deploy script of this blog, on ruby.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env ruby&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# import rugged (the git API for ruby base on libgit2), we need install it by gem first &lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'rugged'&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# import jekyll, install by gem too &lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'jekyll'&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;git_repository&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path-to/git-repository/'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;project_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path-to/project-dir/'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;web_root_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/path-to/web_root_dir/'&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;repo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Rugged&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Repository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;git_repository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# get the list of git tag.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;repo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# build the walker from master &lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;walker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Rugged&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Walker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;repo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;walker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;repo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# walk back and find the newest tag in the master branch &lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;latest_tag&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;walker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# checkout this tag &lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;repo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;workdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;project_dir&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;repo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;checkout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;latest_tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;oid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:strategy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:force&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# build the jekyll project &lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Jekyll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'source'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;project_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'distination'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web_root_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Jekyll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;process&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
Here we use API instead of CLI to access git. 
Maybe a little inconvenience than CLI, but the API is more powerful. 
The libgit2 is a great lib of git API, it support more than a dozen of languages and environments.&lt;/p&gt;

&lt;p&gt;　　
Above examples are the deploy scripts of real using in my work. 
Share the code, and more to the point, share the line of thought. 
We hope to bring help.&lt;/p&gt;</content><author><name></name></author><summary type="html">　　 For web project or other server side project, we need auto-deploy. Here, we take some examples to show how to make a auto-deploy script with post-update of git. We using different languages and different ways. We hope to bring help.</summary></entry><entry><title type="html">Amazing CSS Variables</title><link href="https://www.fenzland.com/en/coding/css/css-variable" rel="alternate" type="text/html" title="Amazing CSS Variables" /><published>2018-11-26T20:12:21+08:00</published><updated>2018-11-26T20:12:21+08:00</updated><id>https://www.fenzland.com/en/coding/css/css-variable</id><content type="html" xml:base="https://www.fenzland.com/en/coding/css/css-variable">&lt;p&gt;　　
We eagerly need variables in CSS by long ages. 
Now, with build systems, we can use variables with Sass and Less. 
What CSS Variables will bring to us? Something Sass/Less never give us.&lt;/p&gt;

&lt;h3 id=&quot;dynamic&quot;&gt;Dynamic&lt;/h3&gt;
&lt;p&gt;　　 
The places of variables in Sass/Less, will token by constants after build. 
Therefore, you can not change them. They are not real variables. 
On the other hand, CSS Variables is real variables, 
you can change them by @media or JS.&lt;/p&gt;

&lt;h3 id=&quot;better-scope-role&quot;&gt;Better Scope Role&lt;/h3&gt;
&lt;p&gt;　　
CSS Variables, also named &lt;em&gt;Custom Properties&lt;/em&gt;, they don’t like variables in Sass/Less, 
but like normal CSS Properties. 
The scope of variables in Sass/Less is between a pair of &lt;code class=&quot;highlighter-rouge&quot;&gt;{}&lt;/code&gt;. 
CSS Variables, however, like other CSS Properties, 
defined in CSSOM and bing to DOM with selector. 
and available in the DOM element and its children (and children of children, and children of children of children…).
So the scope is depend on DOM tree. That’s impossible in Sass/Less.&lt;/p&gt;

&lt;h3 id=&quot;componentization-and-custom-properties&quot;&gt;Componentization and Custom Properties&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-css&quot; data-lang=&quot;css&quot;&gt;&lt;span class=&quot;nt&quot;&gt;button&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;min-width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--button-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1rem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--button-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1rem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;main&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--button-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
In this example, we set size of buttons with &lt;code class=&quot;highlighter-rouge&quot;&gt;--button-size&lt;/code&gt;. 
If it’s undefined, use the default value &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt;.
This rule apply on all &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt;s in the page. 
And we set &lt;code class=&quot;highlighter-rouge&quot;&gt;--button-size: 1.5&lt;/code&gt; on selector &lt;code class=&quot;highlighter-rouge&quot;&gt;body&amp;gt;main&lt;/code&gt;. 
So the variable will apply on &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;main&amp;gt;&lt;/code&gt; directly belongs to &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;body&amp;gt;&lt;/code&gt; and it’s children. 
As we see, height of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt;s in this &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;main&amp;gt;&lt;/code&gt; will be &lt;code class=&quot;highlighter-rouge&quot;&gt;1.5rem&lt;/code&gt;, 
and the height of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt;s outside will be &lt;code class=&quot;highlighter-rouge&quot;&gt;1rem&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;　　
You may say, what? use a variable before declaring?&lt;/p&gt;

&lt;p&gt;　　
Um, let’s think out side the box, the &lt;code class=&quot;highlighter-rouge&quot;&gt;button {}&lt;/code&gt; as a function, and &lt;code class=&quot;highlighter-rouge&quot;&gt;--button-size&lt;/code&gt; as the parameter. 
The function called on rendering of &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt;, and the value defined on &lt;code class=&quot;highlighter-rouge&quot;&gt;body&amp;gt;main&lt;/code&gt; pass in.&lt;/p&gt;

&lt;p&gt;　　
This little example show the basic componentization. 
The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt; is a component with styles inside, and with some parameters. 
We keep not changing styles inside the box, but only change the parameters when use it. 
The CSS Variables becomes the properties of the component.&lt;/p&gt;

&lt;p&gt;　　
It’s hard to make world ordered by this primitive componentization. 
Fortunately, we have Web Components.&lt;/p&gt;

&lt;h3 id=&quot;work-with-web-components&quot;&gt;Work with Web Components&lt;/h3&gt;
&lt;p&gt;　　
In Custom Elements, styles are stand alone with outside. 
But the CSS Variables can penetrate the boundary.&lt;/p&gt;

&lt;p&gt;　　
Here is a example.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-css&quot; data-lang=&quot;css&quot;&gt;&lt;span class=&quot;nd&quot;&gt;:host&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--button-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;button&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1rem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;min-width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--button-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1rem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--button-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1rem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
Here, we set size of buttons just like previous. 
And, we declare &lt;code class=&quot;highlighter-rouge&quot;&gt;--button-size&lt;/code&gt; on the &lt;code class=&quot;highlighter-rouge&quot;&gt;:host&lt;/code&gt;, but not &lt;code class=&quot;highlighter-rouge&quot;&gt;--font-size&lt;/code&gt;. 
The difference is: the variable &lt;code class=&quot;highlighter-rouge&quot;&gt;--font-size&lt;/code&gt; can be defined on any layer outside, 
but the &lt;code class=&quot;highlighter-rouge&quot;&gt;--button-size&lt;/code&gt; must be defined on the custom element itself. 
We can use two ways on different scene.
The way like &lt;code class=&quot;highlighter-rouge&quot;&gt;--button-size&lt;/code&gt;, is the common way to defined a parameter of element. 
And the way like &lt;code class=&quot;highlighter-rouge&quot;&gt;--font-size&lt;/code&gt;, can use to access global configurations.&lt;/p&gt;

&lt;h3 id=&quot;interaction-with-js&quot;&gt;Interaction with JS&lt;/h3&gt;
&lt;p&gt;　　
Here are the API of CSS properties (include Custom Properties) for JS.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;CSSStyleDeclaration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;/*
 * The style can be a style object of a DOM element：
 *   document.body.style
 *
 * And can be a CSS rule：
 *   document.styleSheets[0].cssRules[0].style
 *
 * Also can be a computed result (read only):
 *   getComputedStyle( element, )
 */&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// get value of a CSS property, (always got a string, even the style is undefined)&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getPropertyValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'--foo'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// set a property (the value always be converted to a string)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'--foo'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// remove a property&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;removeProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'--foo'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
This is the better way to access CSS properties then read or assign the properties of the style object.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// good&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'z-index'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'float'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'left'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// bad&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;zIndex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cssFloat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'left'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
Here is a situation that we get the position of mouse by JS, and read the layout. 
Then make some calculations with JS. And set some value to styles. 
That’s dreadful, JS has a hand in the layout, break the separation of concerns. 
And when JS read the layout, unnecessary reflow will happen.&lt;/p&gt;

&lt;p&gt;　　
Using CSS Variables, the problems solved perfectly. 
You just need to pass the position from JS to CSS Variables. 
And do nothing else in JS. In css, using &lt;code class=&quot;highlighter-rouge&quot;&gt;calc()&lt;/code&gt; to take layout calculations. 
The separation of concerns is obeyed. Unnecessary reflows are prevented too.&lt;/p&gt;

&lt;h3 id=&quot;data-types&quot;&gt;Data Types&lt;/h3&gt;
&lt;p&gt;　　
In CSS, the basic data types are number(with unit), string, keyword, color and so on, and list, besides. 
All of them can be the type of CSS Variables.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-css&quot; data-lang=&quot;css&quot;&gt;&lt;span class=&quot;nc&quot;&gt;.foo&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--an-int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--a-float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--with-unit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--a-color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--a-string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;'foobar'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--a-keyword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--a-list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--another-list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;green&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--more-another-list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1em&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
You can combine them and calculate. 
You must pay attention to the unit when calculating. 
To the lists, you need assume &lt;code class=&quot;highlighter-rouge&quot;&gt;,&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; as elements of list just like &lt;code class=&quot;highlighter-rouge&quot;&gt;1px&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-css&quot; data-lang=&quot;css&quot;&gt;&lt;span class=&quot;nc&quot;&gt;.foo&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c&quot;&gt;/* basic using */&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;z-index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--an-int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--a-float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--with-unit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--a-color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;font-family&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--a-string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--a-keyword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--a-list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--more-another-list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	
	&lt;span class=&quot;c&quot;&gt;/* complex using */&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hsla&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--an-int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--with-unit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--an-int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;20%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;box-shadow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--a-list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5px&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--another-list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2px&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4px&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--more-another-list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;work-with-animation-and-transition&quot;&gt;Work with Animation and Transition&lt;/h3&gt;

&lt;p&gt;　　
Unfortunately, there is no browser support transition of CSS Variables. 
We can only use animation and transition on normal CSS properties.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-css&quot; data-lang=&quot;css&quot;&gt;&lt;span class=&quot;nc&quot;&gt;.foo&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--lightness&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;80%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hsla&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--lightness&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hsla&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;--lightness&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	
	&lt;span class=&quot;c&quot;&gt;/* Not supported */&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;--lightness&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;500ms&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;c&quot;&gt;/* Supported */&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;transition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;500ms&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;background-color&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;500ms&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nc&quot;&gt;.foo&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;:hover&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;py&quot;&gt;--lightness&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;50%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;　　
Therefore, some exciting idea cannot achieve. 
Such as change hue and lightness by different speed, 
change color of gradient background… 
Let’s just await for future.&lt;/p&gt;</content><author><name></name></author><summary type="html">　　 We need variables in CSS. Compare with variables in Sass/Less, the CSS Variables is dynamic, have better scope rule, can interact with JS. CSS Variables, or as know as Custom Properties, are perfect match with the Web Components.</summary></entry><entry><title type="html">Fenz Cube</title><link href="https://www.fenzland.com/en/magic-cube/fenz-cube" rel="alternate" type="text/html" title="Fenz Cube" /><published>2018-11-26T20:08:32+08:00</published><updated>2018-11-26T20:08:32+08:00</updated><id>https://www.fenzland.com/en/magic-cube/fenz-cube</id><content type="html" xml:base="https://www.fenzland.com/en/magic-cube/fenz-cube">&lt;p&gt;　　
Thanks for &lt;a href=&quot;https://threejs.org&quot;&gt;Three.js&lt;/a&gt; and &lt;a href=&quot;https://www.khronos.org/webgl&quot;&gt;WebGL&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;　　
Here is the hyperlink: &lt;a href=&quot;https://mc.fenzland.com&quot;&gt;Fenz Cube&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;　　
Fenz Cube is developed on JS, and using ES6, includes ES6 modules, and without Webpack。
So it cannot run on old browsers, such as IE, even FireFox、Edge and Chromium &amp;lt; 63.
Supporting browsers are Chrome, Chromium, Safari, Opera, Chinese 360, QQ (with in Chromium 63+)。&lt;/p&gt;

&lt;p&gt;　　
Fenz Cube support both PC and mobile. 
On PC client, click the faces to twist the cube. 
Left click for anticlockwise twists, and the right click for clockwise twists. 
With Shift, Ctrl and Alt, we can do more different twists. 
Drag the middle button to rotate the view, but not left or right button by default. 
That can prevent misoperations. However, if you don’t like this, you can configure it on settings.
Your settings will store on your localStorage in your browser. 
So the program will remember your preference.
There are some other settings, include some display settings.
One of them is showing backside. Turn on it, you can see the backside of cube with out rotating the view.
And you can click too twist the cube,&lt;/p&gt;

&lt;h3 id=&quot;web-component&quot;&gt;Web Component&lt;/h3&gt;
&lt;p&gt;　　
Now, Fenz Cube support Web Component. 
What you need to do is import the JS module, 
and use &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;fenz-cube&amp;gt;&lt;/code&gt; HTML tag wherever you like in your page.&lt;/p&gt;

&lt;script type=&quot;module&quot; src=&quot;https://mc.fenzland.com/fenz-cube.js&quot;&gt;&lt;/script&gt;

&lt;fenz-cube puzzle=&quot;https://mc.fenzland.com/puzzles/Cubic|0|1.js&quot;&gt;&lt;/fenz-cube&gt;

&lt;p&gt;The codes are：&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-css&quot; data-lang=&quot;css&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;!&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;script&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&quot;module&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&quot;https://mc.fenzland.com/fenz-cube.js&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;lt;!&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;insert&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;cube&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;fenz-cube&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;puzzle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&quot;https://mc.fenzland.com/puzzles/Cubic|0|1.js&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;fenz-cube&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;</content><author><name></name></author><summary type="html">　　 This is a online cube game developed on JS, powered by Three.js and WebGL, depend on ES6 modules, support both PC and mobile.</summary></entry></feed>